echo 1.txt | awk 'BEGIN{FS="\t"} {printf "%s\t%s\n", $2, $1}'
Search This Blog
Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts
Shell scripting: enclose all lines with quotes
- Using awk:
awk '{print "\""$0"\""}' input.txtor if ignore empty lines:awk 'NF {print "\""$0"\""}' input.txt -
Or using sed:
sed -i.bak 's/^/"/; s/$/"/' input.txt
or if ignore empty lines:sed -i.bak 's/^..*$/"&"/' input.txt
awk: print the last two columns
cat file | awk '{print $(NF-1) $NF}'
or
awk '{print $(NF-1) $NF}' file
shell script: read lines from file in a while loop
- by piping cat:
cat /path/to/file | while read line do echo $line done - by redirecting the file:
while read line do echo $line done < /path/to/file - by using awk:
awk '{print $0}' /path/to/file - by using head an tail:
f=/path/to/file total=$(wc -l $f) n=0 while [ $n -lt $total ] do let n++ line=$(head -n $n $f | tail -1) echo ${line} done
awk: add a column based on the test of existing columns
E.g. we have a text file, numbers.txt, see below:
and we want to add the 3rd column, its value is yes/no. If the sum of the 1st column and the 2nd column is greater than 3, the value of the 3rd column is yes, otherwise no.
The following command:
1 2
3 4
5 6
and we want to add the 3rd column, its value is yes/no. If the sum of the 1st column and the 2nd column is greater than 3, the value of the 3rd column is yes, otherwise no.
1 2 no
3 4 yes
5 6 yes
The following command:
awk '{printf "%s %s %s\n",$1,$2,($1+$2>3)?"yes":"no"}' numbers.txt > new_numbers.txt
will do the work.awk: append a column that sums up existing columns
E.g. we have a text file, numbers.txt, see below:
and we want to add the 3rd column which is the sum of values of the first and the second columns.
The following command:
Or, the following command do the same thing:
1 2
3 4
5 6
and we want to add the 3rd column which is the sum of values of the first and the second columns.
1 2 3
3 4 7
5 6 11
The following command:
awk '{print $1 " " $2 " " int($1+$2)}' numbers.txt > new_numbers.txt
will do the work.Or, the following command do the same thing:
awk '{printf "%s %s %d\n",$1,$2,int($1+$2)}' numbers.txt > new_numbers.txt
Subscribe to:
Posts (Atom)