Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract lines with specific patterns in separate files

The input file has the following lines and segregate these using the 2nd field '+' symbol lines in one file and '-' symbol lines in another file:

24 +  I am the Five man    
22 -  Who are you?  The new number two!    
51 +  . . . And four on the floor    
42 +    
16 -  Who is number one?    
33 -  I three you.

is it possible when the $2 is '+', a=$1+500 and b=$1-500 at the same time when the $2 is '-', a=$1-500 and b=$1+500? 'a' and 'b' are the new variables.

like image 417
doc Avatar asked Nov 30 '22 12:11

doc


2 Answers

Another option

awk 'BEGIN{m["+"]="plus.txt";m["-"]="minus.txt"} $2 ~ /^[+-]$/{print>>m[$2]}' 
like image 89
iruvar Avatar answered Dec 04 '22 04:12

iruvar


With Perl:

perl -lne '/^\d+ -/ && print(STDERR) || print' input 2> minus > plus

in a slightly different form:

perl -lpe 'select(/^\d+ -/?STDERR:STDOUT)' input 2> minus > plus

Also possible using tee:

tee >(sed -n '/^[0-9]* -/p' > minus) < input | \
   sed -n '/^[0-9]* +/p' > plus
like image 27
perreal Avatar answered Dec 04 '22 03:12

perreal