Extract one word after a specific word on the same line [duplicate]
Asked Answered
Q

4

18

How can I extract a word that comes after a specific word in Linux (csh)? More precisely, I have a file which has a single line which looks like this:

[some useless data] --pe_cnt 100 --rd_cnt 1000 [some more data]

I want to extract the number 100 which is after the --pe_cnt word. I cannot use sed as that works only if you want to extract an entire line. Maybe I can use awk?

Also, I have multiple files that have different values instead of 100 so I need something that extracts the value but doesn't depend on the value.

Qulllon answered 28/6, 2013 at 18:18 Comment(1)
"awk" is one possibility. For example, echo 5 4 | awk '{ print $2 }' should display "4".Feoffee
L
33

With awk:

awk '{for(i=1;i<=NF;i++) if ($i=="--pe_cnt") print $(i+1)}' inputFile

Basically loop over each word of the line. When you find the first you are looking for, grab the next word and print it.

With grep:

grep -oP "(?<=--pe_cnt )[^ ]+" inputFile
Legendre answered 28/6, 2013 at 18:23 Comment(9)
You need to add a space at the end of the lookbehind pattern in grep.Somatology
Yep sorry didn't test it. Thank you! :)Legendre
N.B. grep on Mac OSX 10.8 doesn't have -P.Desberg
JS웃: Thanks a lot for the quick reply. The first command worked perfectly! The second command did not give any output though.Qulllon
@Qulllon Sorry I had missed a space in the look behind which was corrected by ctn. It should work now.Legendre
Works great! But can you explain the grep regexp, in particular what (?<=--pe_cnt ) does?Keeping
Nevermind, it's a perl regexp using look around assertions, as I found here.Keeping
The sed answer is more efficient and more idiomatic than this answer, and should probably be the most upvoted, if not the accepted answer.Norahnorbert
your answer helped me a lot. Thank youBlindworm
S
6

You can use sed. Just make a group of want you want to match and replace the whole line with the group:

sed -n 's/^.*pe_cnt\s\+\([0-9]\+\).*$/\1/p' file
Somatology answered 28/6, 2013 at 18:21 Comment(1)
There was an error in the pattern previously but it's fixed now.Somatology
I
1

If there is a single-space character between --pe_cnt and 100, you may be able to use lookahead and lookbehind assertions

grep -oP '(?<=--pe_cnt\s)\d+(?=\s+--rd_cnt)'
Iconolatry answered 28/6, 2013 at 18:29 Comment(2)
Probably don't even need the --rd_cnt, it doesn't necessarily matter what the following options areDesberg
Ok. Thanks for the tip. I'm a newbie to Linux so these tips help a lot.Qulllon
E
0

This may be the shortest version:

grep -oP 'pe_cnt \K[^ ]+' file
Egon answered 5/3, 2021 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.