Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print from a string to the end of line 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
0
0
PL
hello
input from ps can be:
user 29976 29787 2 16:45 ? 00:00:00 rsync -H --timeout=60 --force -av --no-owner --no-group --log-file=/tmp/rsync.1448639132.log --include-from=/tmp/sycnzKVqT ....
but options can also be in another order, like:
user 29976 29787 2 16:45 ? 00:00:00 rsync -H --timeout=60 --force -av --log-file=/tmp/rsync.1448639132.log --no-owner --no-group --include-from=/tmp/sycnzKVqT ....

i need to get what log name is used the the running rsync

i could do it using awk if the position is always the same but don't know how to get it when it changes...

I was thinking to get all from field beginning with --log-file to the end of line, then i could easily get log file name using awk -F"[=| ]" ....

thx in advance for a hint.
 
Hi

You mean like this ?
Code:
ps | awk -F '[= ]' '[teal]{[/teal][b]for[/b][teal]([/teal][navy]i[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal][navy]i[/navy][teal]<=[/teal]NF[teal];[/teal][navy]i[/navy][teal]++)[/teal][b]if[/b][teal]([/teal][navy]$i[/navy][teal]==[/teal][i][green]"--log-file"[/green][/i][teal]){[/teal][b]print[/b][navy]$[/navy][teal]++[/teal][navy]i[/navy][teal];[/teal][b]break[/b][teal]}}[/teal]'
Yes, that seems to be the simplest solution, supposing the log file name will never contain space or equal sign.


Feherke.
feherke.ga
 
Hi,

I'm sure feherke's is much more efficient, but I came up with this:

ps | awk -F'--log-file=' '/log-file/{print $2}' | awk '{print $1}'

Thanks,

John
 
Hi

A short off-topic one with GNU [tt]grep[/tt] inspired by John's solution :
Code:
ps | grep -Po '(?<=--log-file=)\S+'
I wish Awk to understand PCRE once.

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top