4

I want to subtract the second line with the first line. The file is like this

tmptxt

A B 1 2 3 4 
C D 9 8 7 6

The desired output is

8 6 4 2

How to do this in awk?

I managed to output for a single column only:

awk '{$temp=$3-prev3; prev3=$3}{print $temp}'
1
  • 1
    @John1024I managed to output for single column awk '{$temp=$3-prev3; prev3=$3}{print $temp}'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk '{for(i=3;i<=NF;i++){$temp[i]=$i-prev; prev=$i;print $temp[i]}}'
    – nabilishes
    Nov 23, 2017 at 5:08

3 Answers 3

4

A solution with awk

awk '
    NR==1 {split($0,a)}
    NR==2 {split($0,b)}
    END {for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]}
' input.txt

gives a result of

0 0 8 6 4 2

Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.

awk '
    NR==1 {split($0,a)}
    NR==2 {split($0,b)}
    END {
        for(i=1;i<=NF;i++)
        if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
        printf "%d ", b[i]-a[i]
    }
' input.txt

gives a result of

8 6 4 2
5
  • 1
    thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
    – nabilishes
    Nov 23, 2017 at 6:11
  • @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
    – etopylight
    Nov 23, 2017 at 6:52
  • Its GNU Awk 3.1.7
    – nabilishes
    Nov 23, 2017 at 6:54
  • 1
    @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
    – etopylight
    Nov 23, 2017 at 7:09
  • Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
    – nabilishes
    Nov 23, 2017 at 7:23
2

Alternative Python solution:

python -c 'import sys; f=open(sys.argv[1],"r"); 
print(" ".join(str(int(d2)-int(d1)) 
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file

The output:

8 6 4 2
0

below command also provides same output. l.txt contains the input which you have provided in question

for i in {1..6}; do awk -v i="$i" 'BEGIN{sum=0}{sum=$i-sum}END{print sum}' l.txt ; done| tr "\n" " " | awk '{$1="";$2="";print $0}' | sed "s/^ //g"

output

8 6 4 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.