Need to print the next word from the same line based on grep string condtion match.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to print the next word from the same line based on grep string condtion match.
# 1  
Old 01-24-2015
Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match.

Actual requirement is need to print the next word from the same line based on grep string condtion match.

Code:
 File :Java.lanag.xyz......File copied completed : abc.txt


Code:
Ouput :abc.txt



I have used below mentioned command

Code:
grep -w “File copied completed” *.log



Getting output as complete line “Java.lanag.xyz......File copied completed : abc.txt”,but need only file name "abc.txt"

Can you please share the command which one can pull only file name from the log file string.
# 2  
Old 01-24-2015
Hi,
with GNU grep and only GNU grep:
Code:
grep -Po '(?<=File copied completed)\W*\K[^ ]*'

Either, it is not possible with other grep that not support regex perl engine.
Regards.
# 3  
Old 01-24-2015
Thanks for the update.

I have executed the command which provided and it's displaying only one character after matching the string which passed on grep command.

Code:
rep -Po '(?<=File copied completed:)\W*\K[^ ]' sample.log

sample.log have the data
Code:
 Java.lanag.xyz......File copied completed:Report1 

Out put is coming
Code:
R

and it should be
Code:
Report1

# 4  
Old 01-24-2015
It looks like the field you want is always at the end of the line?

If that is the case this would work

Code:
$ echo "Java.lanag.xyz……File copied completed:Report1" | awk -F":" '$0~/File copied completed/{print $NF;exit;}'
Report1

Note that the exit will ensure only the first match is returned if there are multiple occurrences of lines having the pattern.


A more generic solution, that extracts the word right after your pattern, no matter whether it is last field or not.

Code:
sed -n -e 's/.*File copied completed://p' infile | sed -e 's/^[ \t]*//' | awk '{print $1}'


Last edited by senhia83; 01-25-2015 at 12:02 AM..
# 5  
Old 01-25-2015
If you are using awk anyhow, then why that pipe organ? Try
Code:
awk '{sub(/^.*File copied completed[ \t]*:[ \t]*/,""); print $1}' file
abc.txt
Report1

# 6  
Old 01-25-2015
Quote:
Originally Posted by siva83
Thanks for the update.

I have executed the command which provided and it's displaying only one character after matching the string which passed on grep command.

Code:
rep -Po '(?<=File copied completed:)\W*\K[^ ]' sample.log

sample.log have the data
Code:
 Java.lanag.xyz......File copied completed:Report1 

Out put is coming
Code:
R

and it should be
Code:
Report1

You forgot the '*' final:
my syntax:
Code:
grep -Po '(?<=File copied completed:)\W*\K[^ ]*'

This User Gave Thanks to disedorgue For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match string from two files and print line

Hi, I have been trying to find help with my issue and I'm thinking awk may be able to do it. I have two files eg file1.txt STRING1 230 400 0.36 STRING2 400 230 -0.13 STRING3 130 349 1 file2.txt CUFFFLINKS 1 1394 93932 . + STRING1 CUFFFLINKS ... (9 Replies)
Discussion started by: zward
9 Replies

2. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

3. Shell Programming and Scripting

How to match the first word and print only that line in UNIX?

Below is the file DISK-A 109063.2 49 31 40.79 DISK-B 110058.5 49 44 57.07 DISK-c 4402.4 2 1 2.14 from the file, i want to search for 'DISK-A' and print only that line with the first word matching to DISK-A and the output should skip DISK-A. Desired Output: (If i'm... (2 Replies)
Discussion started by: web2moha
2 Replies

4. Shell Programming and Scripting

Print string after the word match

Hi, I have the logs : cat logsx.txt 744906,{"reportingGroups":,"version":"2.0"} 678874,{"reportingGroups":,"version":"2.0"} 193571,{"reportingGroups":,"version":"2.0"} 811537,{"reportingGroups":,"version":"2.0"} 772024,{"reportingGroups":,"version":"2.0"}... (5 Replies)
Discussion started by: justbow
5 Replies

5. Shell Programming and Scripting

Command to grep a word and print the whole line splitted into many

Hi, I need to search a word in the java file. Assume the line in the java file is, (the line was splitted into 3 lines) 1.operationContext.sendFeedback(2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr 3.ovide Valid pair(s): "+pairType); When i grep for the word... (6 Replies)
Discussion started by: tulasiram
6 Replies

6. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

7. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

8. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

9. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies
Login or Register to Ask a Question