0

Data:

 61      -3.5527      1.00000
 62      -3.4695      1.00000
 63      -3.4545      1.00000
 64      -3.1462      1.00000
 65      -1.4512      0.00000
 66      -1.1737      0.00000
 67      -0.8792      0.00000
 61      -3.5590      1.00000
 62      -3.4763      1.00000
 63      -3.4557      1.00000
 68      -3.1533      1.00000
 69      -1.4382      0.00000
 70      -1.1616      0.00000
 71      -0.8638      0.00000

What i expect:

file1:

 64      -3.1462      1.00000
 68      -3.1533      1.00000

file2:

 65      -1.4512      0.00000
 69      -1.4382      0.00000

I want to get file1 and file2 by using simply bash shell and awk. How do I find the two lines where the values of the 3rd column are changed from 1 to 0? Thx in advance!

3
  • Some details might avoid the usual perl/grep/python/whatnot answers on an awk targeting question: What are the separaters between the columns? Tabs, spaces, mixed? Do you seek the equivalent of say cat Data|grep -e '^64 ' > file1 ? Or is/are there sometimes space/s at the beginngin of the Data lines? Commented Jun 2, 2016 at 5:38
  • 5
    You have to explain what the criteria are for a record to get into one or the other file, or we have to guess. It would also help to show your own efforts, as currently this looks like you expect somebody else to do all your work. Commented Jun 2, 2016 at 5:39
  • You're right. Benjamin Sorry about that. I edited the post. Commented Jun 2, 2016 at 5:51

4 Answers 4

6

Try this awk script:

NR == 1 { last = $3; }
last == 1 && $3 == 0 { print previous > "file1" ; print $0 > "file2" ; }
last != $3 { last = $3; }
{ previous = $0 }

This works as follows: If the line number is 1, record the third column as last. If last equals 1 and the third column of the current line equals 0, print the content of the variable previous to file1 and the current line to file2. If the value of last is not equal to the third column of the current line, change the value of the variable. Finally, record the current line as previous, for use in the second rule.

Since it isn't really necessary to intialize last, or to check whether the value of the third column has changed, the script can be shortened to:

last == 1 && $3 == 0 { print previous > "file1" ; print $0 > "file2" ; }
{ last = $3; previous = $0 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Michael It works! I'm wondering if previous and $0 are system variable... Could you explain little about your code?
1

Long version

awk '
  NR>1 && last==1 && $3==0 {
    print last_line > "file1"
    print $0 > "file2"
  }
  {
     last=$3
     last_line=$0
  }
' input.txt

Short version

awk 'NR>1&&x==1&&$3==0{print y>"file1";print $0>"file2"}{x=$3;y=$0}' input.txt

Comments

0
 awk  'BEGIN{prev=1; prevRec=""} prev == 1  && prevRec != "" && $3 == 0{print $0 > "file1";print prevRec > "file2"} {prev = $3; prevRec=$0}' file

1 Comment

While this might be technically correct you should explain what it does
0
awk '{ tmp=match($3,/[1][.]?????/) ; if (tmp) { lineone=$0 ; newzero=0 ;} else if ( newzero <1) { onechanged[lineone]=1 ; zerochanged[$0]=1 ; newzero=1} } END { print "File1 is: " ; for (key in onechanged){ print key } ; print "File2 is:" ; for (key in zerochanged){ print key }}' test.txt

output is :

File1 is:
 64      -3.1462      1.00000
 68      -3.1533      1.00000
 File2 is:
 69      -1.4382      0.00000
 65      -1.4512      0.00000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.