Example of awk, if while in shell

Source: Internet
Author: User

1. If while command is written in one line
While read a b;do echo $a $b;d one < Aa.txt
12 13 14
Cat Aa.txt
12 13 14
if [[$i-eq 0 | | $i-eq 2]]; then env=10.79.40.146:80; else env=10.79.40.147:80; Fi
2. Awk merges two files
Account Cdr
Cat Account
Zhang San |000001
John Doe |000002
Cat Cdr
000001|10
000001|20
000002|30
000002|15
The result of the merge is
Zhang San |000001|10
Zhang San |000001|20
John Doe |000002|30
John Doe |000002|15
Command: Awk-f \| ' Nr==fnr{a[$2]=$0;next}{print a[$1] ' | $ '
Explain:
NR (number of Record) indicates how many rows of data were read by awk
FNR (number of Record in file) is similar to NR, unlike awk, where each new file is opened and FNR is re-accumulated starting from 0
3. While stitching file
#将afile文件中的前三行与bfile中的前四行拼接在一起
While Read-u3 i && read-u4 j;do
echo $i $j
Done 3<afile 4<bfile
4. The grouping sum of awk
Cat File.txt
Institution name, name, status, amount
Jiangnan Reminghe Failure 420
Jiangnan Reming Success 300
Jiangnan Gou Xiu Hua Success 60
Baita Gou should be flat failure 360
Baita Gou-June defeat 180
Water View Gou Hui success 240
Water view Gou should east failure 60
Water View Gou failed training 120
Water View Gou Chengxun Success 360
Rocky Beach Hou Chengming Failure 300
Rocky Beach Hou Xingwen Success 360
Rocky Beach Hou Xinghua Success 120
Requirements:
1, the total amount counts and sums;
2, according to the status of the classification count and sum;
3. Count and sum by institution name and status
The results are as follows:
12 2880
Success 6 1440
Failure 6 1440
Jiangnan Success 2 360
Jiangnan Failure 1 420
Baita Failure 2 540
Water View Success 2 600
Failure of Water View 2 180
Pebble Beach Success 2 480
Rocky Beach Failure 1 300
Command:
#!/bin/bash
echo "Total amount count and sum"
awk ' {if (nr!=1) {total+=$4;sum++}}end{print total, sum} ' file.txt
echo "Count and sum by status"
awk ' {if (nr!=1) {a[$3]+=$4;b[$3]++}}end{for (i in a) {print i,a[i],b[i]}} ' file.txt
echo "Count and sum by organization and state"
#NR!=1 Skip Header,a[$1 "" $ $) add a space between 1 3 to facilitate the final sorting
awk ' {if (nr!=1) {a[$1 "" $3]+=$4;b[$1 "" $3]++}}end{for (i in a) {print i,a[i],b[i]}} ' File.txt|sort-k 4

Example of awk, if while in shell

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.