Reputation: 147
Imagine i have a log file or a text file like below
16 Dec 2014 11:20:00 [INFO] com.example.Test.java PlaneName: JetAirways360 This flight just cleaned up
16 Dec 2014 11:22:01 [INFO] com.example.Test.java PlaneName: JetAirways360 This flight is in queue
16 Dec 2014 11:23:02 [INFO] com.example.Test.java PlaneName: JetAirways360 This flight passengers loaded
16 Dec 2014 11:24:03 [INFO] com.example.Test.State.java PlaneName: JetAirways360 This flight ready to take off
16 Dec 2014 11:25:00 [INFO] com.example.Test.State.java PlaneName: JetAirways360 This flight took off
17 Dec 2014 11:25:00 [INFO] com.example.Test.java PlaneName: JetAirways360 This flight returned back
Now imagine i this log file is fully filled off lot of flight information. it is heard to find a particular flight information. Now i would like to grep to see following details
16 Dec 2014 11:20:00 This flight just cleaned up
16 Dec 2014 11:22:01 This flight is in queue
16 Dec 2014 11:23:02 This flight passengers loaded
16 Dec 2014 11:24:03 This flight ready to take off
16 Dec 2014 11:25:00 This flight took off
17 Dec 2014 11:25:00 This flight returned back
How to do this using grep and sed command?.
Upvotes: 0
Views: 118
Reputation: 10039
sed -n '/\[.*JetAirways360/ s///p' YourFile
should do your job based on your sample
Upvotes: 1
Reputation: 147
grep 'JetAirways360' lofFile | sed -e 's/\(.*\) \[INFO.*JetAirways360\(.*\)/\1 \2/g'
To explain this.
Details:
Upvotes: 0
Reputation: 8402
There are many ways to do this with sed the command below also works.
sed 's#\([[:digit:]]\{2,2\}.*[[:digit:]]\{2,2\}:[[:digit:]]\{2,2\}:[[:digit:]]\{2,2\}\).*PlaneName.*JetAirways360 \(.*\)#\1 \2#g' 'my_log_file'
It marks the first two digits in the line up to the up to the flight time and remembers it. sed then searches the line for JetAirways360 and after this name it marks the remainder of the line. The two patterns are then combined.
Using grep and cut
Another easier trick to do this without using grep or sed is using "cut" (even though this might no pertain to answering your question)
cut -d ' ' -f1-4,9- 'my_log.txt'
from field (column in this case) 1 to 4 is printed and then field 9 onwards
results using "cut"
16 Dec 2014 11:20:00 This flight just cleaned up
16 Dec 2014 11:22:01 This flight is in queue
16 Dec 2014 11:23:02 This flight passengers loaded
16 Dec 2014 11:24:03 This flight ready to take off
16 Dec 2014 11:25:00 This flight took off
17 Dec 2014 11:25:00 This flight returned back
Note: this just cuts the columns you need and output no grepping nor "sedding" is involved.
You can also use grep for a Plane name which will print all lines with the Plane name and then use "cut"
example
grep "JetAirways36" 'my_log.txt'|cut -d ' ' -f1-4,9-
Upvotes: 0
Reputation: 5367
perl -ne 'print if s!.INFO.*Name: JetAirways360!!' file
If you prefer a new command flygrep
:
#!/usr/bin/perl -n
BEGIN{ $fl = shift;}
print if s!.INFO.*Name: $fl !!
Usage: flygrep JetAirways360 file
Upvotes: 0
Reputation: 41460
This is simple to do with awk
file
awk -F" [[]|JetAirways360 " '{print $1,$3}' file
16 Dec 2014 11:20:00 This flight just cleaned up
16 Dec 2014 11:22:01 This flight is in queue
16 Dec 2014 11:23:02 This flight passengers loaded
16 Dec 2014 11:24:03 This flight ready to take off
16 Dec 2014 11:25:00 This flight took off
17 Dec 2014 11:25:00 This flight returned back
Upvotes: 1
Reputation: 58578
This might work for you (GNU sed):
sed -rn '/JetAirWays360/s/(.{21}).{54}/\1/p' file
This saves the first part of the file in a back reference and replaces a portion of the remaining file with it.
Upvotes: 2