Tag Info

Hot answers tagged

4

If your files are not too large to fit in memory, you could use perl to slurp the file: perl -0777pe 's/.*?PAT[^\n]*\n?//s' file Just change PAT to whatever pattern you're after. For example, given these two input files and the pattern 5: $ cat file 1 2 3 4 5 11 12 13 14 15 $ cat file1 foo bar $ perl -0777pe 's/.*?5[^\n]*\n?//s' file 11 12 13 14 15 $ ...


3

GNU grep; cat: { grep -m1 'pattern' && cat || ! cat ./infile } <./infile POSIX sed; cat: { sed -ne'/PATTERN/q;H;1h;$!d;x;p'; cat; } <infile GNU sed; cat: { sed -une'/PATTERN/q;H;1h;$!d;x;p'; cat; } <infile (just add -u) sharing is nice All of the above commands work because the file-descriptor from which they read() is ...


2

Using GNU sed, you can do this: :x;/PATTERN/{s/.*//;:z;N;bz};N;bx For example is we use 7 as the pattern we want to match and input data generated by seq, this will print the numbers 8 to 20 (including 17): seq 20 | sed ':x;/7/{s/.*//;:z;N;bz};N;bx' And this will print 1 to 6: seq 6 | sed ':x;/7/{s/.*//;:z;N;bz};N;bx' As noted in the comments, this ...


1

Pipe the input (cat file in this case) into a basic single-pass of awk first. Then, if pattern is not found (ie. nothing was printed), the process continues on with cat file cat file | { awk -v pat='^a.c$' ' { if( m ) print; else{ if( $0 ~ pat ) m=1 } } END{ exit !m }' || cat file } input: 1 2 abc 4 aXc 6 output ...



Only top voted, non community-wiki answers of a minimum length are eligible