Search code examples
regexlinuxgreppcre

how linux grep -oP print the pattern only


# cat a.file
123123abc file2
274efcc4d8a0e8f61397be46bd4bfa37  file1
321  file1

# file="file1"
# grep -m 1 -oP "\K([\w]+) +$file" a.file
274efcc4d8a0e8f61397be46bd4bfa37  file1

How can I get the output 274efcc4d8a0e8f61397be46bd4bfa37 with the parameter file1 line, and only use grep command without | or other command like awk?

Is there grep -P have some other parameters for only print the match pattern \K([\w]+) such as the result is 274efcc4d8a0e8f61397be46bd4bfa37? Or Is there any implementation to get the result with grep only.


Solution

  • $ file='file1'
    $ grep -m1 -oP '\w+(?= +'"$file"'$)' a.file
    274efcc4d8a0e8f61397be46bd4bfa37
    

    (?=regexp) is a positive lookahead construct which helps you define an assertion without it being part of the matched portion. See Reference - What does this regex mean? for more information on lookarounds.

    I've also placed '\w+(?= +' and "$file" and '$)' next to each other, so that only the shell variable is under double quotes. The $ anchor is used here to avoid partial match, for example file1 vs file1a or file11. If your filename can have regexp metacharacters (for ex: .), then you'll need to use '\w+(?= +\Q'"$file"'\E$)' to match the filename literally.


    Not sure why you do not want to use awk here, it is well suited for this task. String comparison comes in handy to avoid having to deal with regexp escaping.

    awk -v f="$file" '$2==f{print $1; exit}'