sed and awk

Use sed and awk you could print certain lines that meet your query condition.
Here is a command I used to print book titles of my son's reading history from a text file. From the picture, we can tell that book titles are mixed with DVD titles and the author names are listed after titles if there is any.


My task is to pick up titles of books only. Here sed and awk comes handy.
sed -n '/TITLE/p' history.txt | sed -n '/DVD/!p' | awk '{FS="/"; print $1 }' | awk '{FS="        "; print $2}'
By default sed will print anything from the original file.
-n '/TITLE/p' will let sed print only lines contain 'TITLE'.
-n '/DVD/!p' will further suppress lines contains 'DVD'
awk '{FS="/"; print $1 } will remove the author information from title
Further investigate reveal that there are 8 spaces between TITLE and the title of the book,
awk '{FS=" "; print $2}' will print out only the title of the book.

No comments:

Post a Comment

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...