shell - AWK Script along with loop and if condition -
shell - AWK Script along with loop and if condition -
the input file below
827 819 830 826 828 752 756 694 828 728 821 701 724 708 826 842 719 713 764 783 752 828 694 756
#$1
first row , $2 sec row. trying omit rows such 11th row same 3rd row swapped values. basically, every $1 $2 has $2 $1, want omit latter. snippet of data. there many such values in actual dataset.`
i have tried below:
awk -f “ “ ‘{ in cat 686.edges.txt | if [ expr $1 $2 == expr $2 $1 ] #evaluating status file
and
awk -f “ “ ‘{ print $2 $1 }’ >> t.txt else ‘{ print “ not found “ } fi #printing $y $x file
and
awk -f “ “ ‘{ in cat t.txt} | grep -v "$1 $2" 686.edges.txt >> new.txt
i reading inputs t.txt result of previous operation , removing of them main file , writing in new.txt
i unable execute have been getting errors. can evaluate above , right me.
this prints rows unless reverse of row has been seen:
$ awk '!seen[$2" "$1] {print} {seen[$0]=1}' t.txt 827 819 830 826 828 752 756 694 828 728 821 701 724 708 826 842 719 713 764 783
this assumes columns separated space. if separated by, example, tab, minor alter code needed.
to write output new.txt
instead of terminal, use:
awk '!seen[$2" "$1] {print} {seen[$0]=1}' t.txt >new.txt
how works awk
reads in record (row) @ time. each row divided fields (columns). utilize array seen
maintain track of (reversed) rows have been seen.
!seen[$2" "$1] {print}
if reverse of current row has not been seen, print row. (!
awk symbol "not".)
{seen[$0]=1}
mark current row seen.
alternate: omitting rows seen regardless of orderthis omit printing row had been seen either or in reverse order:
awk '0==seen[$0] {print} {seen[$0]=1; seen[$2" "$1]=1}' t.txt >new.txt
solution using multi-dimensional arrays as suggested glenn jackman, if awk supports multi-dimensional arrays, above 2 solutions can written:
awk --posix '!seen[$2,$1] {print} {seen[$1,$2]=1;}' t.txt >new.txt
and
awk '!seen[$1,$2] {print} {seen[$1,$2]=1; seen[$2,$1]=1}' t.txt >new.txt
shellter points out notation supported in original the awk programming language (pages 52-3). on other hand, grymoire describes notation "invalid". so, may not work on versions of awk. is, however, supported gnu awk (linux). because notation required posix, should work in modern awks.
shell awk grep
Comments
Post a Comment