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.

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

I have used below mentioned command

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.

Hi,
with GNU grep and only GNU grep:

grep -Po '(?<=File copied completed)\W*\K[^ ]*'

Either, it is not possible with other grep that not support regex perl engine.
Regards.

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.

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

sample.log have the data

 Java.lanag.xyz......File copied completed:Report1 

Out put is coming

R

and it should be

Report1

It looks like the field you want is always at the end of the line?

If that is the case this would work

$ 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.

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

If you are using awk anyhow, then why that pipe organ? Try

awk '{sub(/^.*File copied completed[ \t]*:[ \t]*/,""); print $1}' file
abc.txt
Report1

You forgot the '*' final:
my syntax:

grep -Po '(?<=File copied completed:)\W*\K[^ ]*' 
1 Like