Shell script, awk subtracts the value of each line of data in file a from the corresponding row of file B and obtains its absolute values.

Source: Internet
Author: User

Thinking of solving problems

The file Shu is the following.
220 34 50 70
553 556 32 21
1 1 14) 98 33

The file Jian is the following.
10
8
2

Want to get the result is the following.
210 24 40 60
545 548 24 13
1 1 12) 96 31

Use awk to get the results you want.

[Email protected] study]# awk ' nr==fnr{a[nr]=$1;next}nr!=fnr{for (i=1;i<=nf;i++) {$i >a[fnr]? $i = $i-a[fnr]: $i =a [fnr]-$i}}1 ' Jian Shu
210 24 40 60
545 548 24 13
1 1 12) 96 31

Analyze the steps above

NR==FNR handles the first file next to the Skip function.
A[nr]=$1
a[1]=10
A[2]=8
a[3]=2

NR!=FNR Processing a second file
for (i=1;i<=nf;i++) traversal output 1,2,3,4
$i >a[fnr]? $i = $i-a[fnr]: $i =a[fnr]-$i This is equivalent to three mesh operations such as 3>1?a=1:a=2 this represents when 3 is greater than 1 o'clock, output 1, otherwise output 2

The second file processes the first line first
$1>a[1]?$1=$1-a[1]:$1=a[1]-$1----> 220 greater than 10, so perform 220-10=210
$2>a[1]?$2=$2-a[1]:$2=a[1]-$2----> 34 greater than 10, so perform 34-10=24
$3>A[1]?$3=$3-A[1]:$3=A[1]-$3----> 50 greater than 10, so perform 50-10=40
$4>a[1]?$4=$4-a[1]:$4=a[1]-$4----> 70 greater than 10, so perform 70-10=60

And then, in the second line of work
$1>a[2]?$1=$1-a[2]:$1=a[2]-$1----> 545 greater than 8, so perform 553-8=545
$2>a[2]?$2=$2-a[2]:$2=a[2]-$2----> 556 greater than 8, so perform 556-8=548
$3>A[2]?$3=$3-A[2]:$3=A[2]-$3----> 32 greater than 8, so perform 32-8=24
$4>a[2]?$4=$4-a[2]:$4=a[2]-$4----> 21 greater than 8, so perform 21-8=13

Last process third row
$1>a[3]?$1=$1-a[3]:$1=a[3]-$1----> 1 less than 2, so perform 2-1=1
$2>a[3]?$2=$2-a[3]:$2=a[3]-$2----> 1 less than 2, so perform 2-1=1
$3>a[3]?$3=$3-a[3]:$3=a[3]-$3----> 14 greater than 2, so perform 14-2=12
$4>A[3]?$4=$4-A[3]:$4=A[3]-$4----> 98 greater than 2, so perform 98-2=96
$4>a[3]?$4=$4-a[3]:$4=a[3]-$4----> 33 greater than 2, so perform 33-2=31

So it's output.
210 24 40 60
545 548 24 13
1 1 12) 96 31

The

Shell script, awk, subtracts the value of each line of data in file a from the corresponding row of file B, resulting in absolute values.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.