how to use sed, awk, or gawk to print only what is matched?

后端 未结 11 490
长情又很酷
长情又很酷 2021-01-30 05:59

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk.

But in my case, I have a regular expression that I want to run

11条回答
  •  既然无缘
    2021-01-30 06:31

    I use perl to make this easier for myself. e.g.

    perl -ne 'print $1 if /.*abc([0-9]+)xyz.*/'
    

    This runs Perl, the -n option instructs Perl to read in one line at a time from STDIN and execute the code. The -e option specifies the instruction to run.

    The instruction runs a regexp on the line read, and if it matches prints out the contents of the first set of bracks ($1).

    You can do this will multiple file names on the end also. e.g.

    perl -ne 'print $1 if /.*abc([0-9]+)xyz.*/' example1.txt example2.txt

提交回复
热议问题