Shell scripting: triming text from start to end markers with sed or awk

Let’s assume you have a huge log file looking like this:

some more log
2015-09-07 12:10 some log
some more log
...
2015-09-07 12:11 some log
...
some more log
2015-09-07 12:15 some log
some more log

Let’s assume you are interested only in the part between 2015-09-07 12:10 and 2015-09-07 12:15.

Here is a sed script that will do the job:

sed -n '/2015-09-07 12:10/,/2015-09-07 12:15/p' file.log

Here is also an awk script that does the same job:

#!/bin/sh                                                                       

awk -v "FROM=$1" -v "TO=$2" '($0 ~ FROM) {i=1} ($0 ~ TO) {i=0} (i) {print $0}' $3 

Save it and call it like this:

trim "2015-09-07 12:10" "2015-09-07 12:15" file.log

You can even use regular expressions as markers.

Advertisements
This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s