Extract one word after a specific word on the same line

后端 未结 3 851
半阙折子戏
半阙折子戏 2020-11-27 22:19

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:

[so         


        
相关标签:
3条回答
  • 2020-11-27 22:47

    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)'
    
    0 讨论(0)
  • 2020-11-27 22:56

    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
    
    0 讨论(0)
  • 2020-11-27 23:01

    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
    
    0 讨论(0)
提交回复
热议问题