4

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.

Casey
  • 41,449
  • 7
  • 95
  • 125
doc
  • 45
  • 5
  • is it possible to segregate above lines in two files basing on 2nd field, which is having '+' and '-' symbol using awk... – doc Jun 23 '13 at 09:04
  • 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... – doc Jun 24 '13 at 12:47

7 Answers7

5

Another option

awk 'BEGIN{m["+"]="plus.txt";m["-"]="minus.txt"} $2 ~ /^[+-]$/{print>>m[$2]}' 
iruvar
  • 22,736
  • 7
  • 53
  • 82
5

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
perreal
  • 94,503
  • 21
  • 155
  • 181
4

This solution filters the output to files f1 and f2.

awk '{ if ($2 == "+") print >>"f1"; else if ($2=="-") print >>"f2"; }' datafile
suspectus
  • 16,548
  • 8
  • 49
  • 57
4

This will put the "+" lines in file1 and the others in file2:

awk '{print > ("file" ($2~/+/?1:2))}' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    Hello Ed Morton, now I need to take the 1st field which are having '+' sign and assign to new variables by adding 500 and subtracting 500 of each record. – doc Jun 24 '13 at 13:21
  • What do you mean by "assign to new variables"? Just update your question with your new requirements along with sample input and expected output or open a new question. – Ed Morton Jun 24 '13 at 15:00
2

Code for GNU :

sed '/\S\+\s\++/!D' file > plus.txt
sed '/\S\+\s\++/D' file > minus.txt
captcha
  • 3,756
  • 12
  • 21
2

With awk you would simply do:

awk '$2=="+"{print>"f1";next}{print>"f2"}' file

Demo:

$ cat 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.

$ awk '$2=="+"{print>"f1";next}{print>"f2"}' file

$ cat f1
24 +  I am the Five man
51 +  . . . And four on the floor
42 +

$ cat f2
22 -  Who are you?  The new number two!
16 -  Who is number one?
33 -  I three you.
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • Hello sudo_O, now I need to take the 1st field which are having '+' sign and assign to new variables by adding 500 and subtracting 500 of each record. – doc Jun 24 '13 at 13:19
-1

The question's title is a bit unclear, I'll edit it in a moment. Meanwhile here's the answer:

awk '/^[0-9]+ \+/{print > "a"} /^[0-9]+ -/{print > "b"}'
Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51