bash - Renumbering of a column and replace of lines in the file -
i have file following. 1abc lines, want replace 1.line 3.line, 2.line 1.line, 3.line 4.line , 4.line 2.line. want same thing 2abc lines (in real file have 1abc,2abc,3abc...1000abc lines). after replacing lines, should renumber 3.column. how can (i have preserve spacing between columns in output file)?
input file:
1abc c1 1 0.349 1abc h2 2 0.123 1abc o1 3 0.217 1abc h4 4 0.180 2abc c1 5 2.015 2abc h2 6 0.573 2abc o1 7 1.929 2abc h4 8 1.867 requested output:
1abc h2 1 0.123 1abc h4 2 0.180 1abc c1 3 0.349 1abc o1 4 0.217 2abc h2 5 0.573 2abc h4 6 1.867 2abc c1 7 2.015 2abc o1 8 1.929
following script change third column of input file number require , output should sorted third field.
- script : torun.sh
filename="input.txt"
cat input.txt | while read line key=`echo $line | awk '{print $3}'` res=`echo $key % 4 | bc` newvalue=$key if [ $res -eq "0" ] newvalue=`expr $key - 2` fi if [ $res -eq "1" ] newvalue=`expr $key + 2` fi if [ $res -eq "2" ] newvalue=`expr $key - 1` fi if [ $res -eq "3" ] newvalue=`expr $key + 1` fi echo $line | awk -v v1="$newvalue" '{$3=v1; print}' done
how run : ./tunrun.sh | sort -k 3
please note awk changes delimiter of column single space. not sure whether multiple space or tab delimiter. can fix script display line correct delimiter once confirm.
Comments
Post a Comment