简体   繁体   中英

how to print file from the match string until EOF

the following command print the file until the match WORD

 awk '1;/WORD/{exit}' file

but how to print the file from the string WORD until the end of file not include the string WORD?

As Etan Reisner says in a comment, there is a nice cookbook of range patterns in this answer . But the simplest way to match from a pattern to the end of a file is:

awk '/WORD/,0' file  

In order to print from the line following the line containing a pattern, we could instead do this:

awk 'found,0;/WORD/{found=1}' file

To also print the part of the first line which matches WORD following WORD, it is only necessary to modify the last action, but it's convenient to replace the regular expression with an explicit call to match in order to set RSTART and RLENGTH:

awk 'found,0;match($0,/WORD/){found=1;print substr($0, RSTART+RLENGTH}'

Range patterns have the form expression expression , and the meaning is to match from the first line which matches the first expression to the first line which matches the last expression, inclusively. expression ,其含义是从与第一个表达式匹配的第一行到与最后一个表达式匹配的第一行(包括两端)匹配。 The range is repeated until the file is fully processed.

In these examples, the second expression always evaluates to 0 (false), so the range never terminates and all lines are matched once the pattern succeeds.

Similarly, another way to solve the "print all lines until a pattern" would be the following, although it is less efficient because it reads the entire file:

awk 'NR==1,/WORD/' file

Also, if the goal is to print up to only the instance of the pattern (as opposed to the complete line containing th pattern, we could produce a simple modification of the original program:

awk 'match($0, /WORD/){print(sub($0,1,RSTART+RLENGTH)); exit}1'

This MIGHT be what you want:

$ cat file                                 
As market-days are wearing late,
And folk begin WORD to tak the gate;
While we sit bousin, at the nappy,
And gettin fou and unco happy,

$ awk '!f && sub(/.*WORD/,""){f=1} f' file
 to tak the gate;
While we sit bousin, at the nappy,
And gettin fou and unco happy,

If text have only one pattern , this gnu awk (gnu due to the RS) will work:

awk -v RS="WORD" 'NR>1' file

It will work as Eds solution, start with first data after WORD and print the rest of the line and all next line to the EOF


This will print the next line after the WORD is found and until EOF
If you need data on the same line after WORD look at Eds answer.

awk 'f;/WORD/{f=1}' file

Example, pattern four

cat file
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten

awk 'f;/four/ {f=1}' file
5 five
6 six
7 seven
8 eight
9 nine
10 ten

This might work for you (GNU sed):

sed '1,/WORD/{/WORD/!d;s//\n/;D}' file

This deletes all the lines up until WORD and then replaces WORD by a newline and delete up until and including the newline. The remaining file is printed as normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM