Ravi Krishna P
Ravi Krishna P

Reputation: 147

How to print more than one String using grep and sed?

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

Answers (6)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -n '/\[.*JetAirways360/ s///p' YourFile

should do your job based on your sample

Upvotes: 1

Ravi Krishna P
Ravi Krishna P

Reputation: 147

grep 'JetAirways360' lofFile | sed -e 's/\(.*\) \[INFO.*JetAirways360\(.*\)/\1 \2/g'

To explain this.

  1. First i am grepping all the log entries which has JetAirways360.
  2. I am using sed. each bracket (.) is what i am trying to get from log. \1 is for first (.) and \2 for second (.*). Its like print statements for string value i cut from original line.

Details:

  1. sed regular expression should contain between this s/ /g
  2. \1 and \2 are the print statements for (.*).
  3. (.) is represented as (.). Escape sequence
  4. s/(.*) [INFO This Explains Put all characters into the parenthesis till you see [INFO (i have put escape character \ for [ and hence [INFO). That's how i got Date selected and printed using \1
  5. [INFO.JetAirways360(.) This explains, After seeing [INFO start ignoring till you see JetAirways360. After which put all remaining character into parenthesis. That's how i got log message selected and printed using \2

Upvotes: 0

repzero
repzero

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

JJoao
JJoao

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

Jotne
Jotne

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

potong
potong

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

Related Questions