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!
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 }
awk 'BEGIN{prev=1; prevRec=""} prev == 1 && prevRec != "" && $3 == 0{print $0 > "file1";print prevRec > "file2"} {prev = $3; prevRec=$0}' file
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
awk '
NR>1 && last==1 && $3==0 {
print last_line > "file1"
print $0 > "file2"
}
{
last=$3
last_line=$0
}
' input.txt
awk 'NR>1&&x==1&&$3==0{print y>"file1";print $0>"file2"}{x=$3;y=$0}' input.txt
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.