Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use awk command to realize multi-file operation in linux

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux how to use the awk command to achieve multi-file operation, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

The code is as follows:

[chengmo@centos5 shell] $awk 'FNR==1 {print "\ r\ n" FILENAME} {print $0}' a.txt b.txt

A.txt

100 wang man

200 wangsan woman

300 wangming man

400 wangzheng man

B.txt

100 90 80

200 80 70

300 60 50

400 70 20

Need to merge to get the result:

100 wang man 90 80

200 wangsan woman 80 70

300 wangming man 60 50

400 wangzheng man 70 20

Awk multi-file operation method 1:

The idea of realization is:

Merge files through external commands, then sort them, and then merge them through awk.

First of all:

The code is as follows:

[chengmo@centos5 shell] $cat a.txt b.txt | sort-n-K1 | awk'{print}'

100 90 80

100 wang man

200 80 70

200 wangsan woman

300 60 50

300 wangming man

400 70 20

400 wangzheng man

Now you need to merge the same processing of the first column into one line, using the "next" statement. For its operation, please refer to awk Multiline merging [introduction to next usage] (common applications 4)

Continue:

The code is as follows:

[chengmo@centos5 shell] $cat a.txt b.txt | sort-n-K1 | awk 'NR%2==1 {fd1=$2 "\ t" $3X next} {print $0 "\ t" fd1}'

100 wang man 90 80

200 wangsan woman 80 70

300 wangming man 60 50

400 wangzheng man 70 20

Several lines need to be merged, often as follows: NR%num and then save the row value, next the line. Print it out on output.

Awk multi-file operation method 2

Realization idea

Open multiple files directly through awk without the help of the third release tool. You can then get the current processing file name through: FILENAME. NR Total record FNR current file record, as well as the total number of parameters passed in by ARGC. ARGV is an array of parameter values.

Take a look at these examples:

The code is as follows:

[chengmo@centos5 shell] $awk 'BEGIN {print ARGC,ARGV [0], ARGV [1], ARGV [2]} {print FILENAME,NR,FNR,$0}' a.txt b.txt

3 awk a.txt b.txt

A.txt 1 1 100 wang man

A.txt 2 2 200 wangsan woman

A.txt 3 3 300 wangming man

A.txt 4 4 400 wangzheng man

B.txt 5 1 100 90 80

B.txt 6 2 200 80 70

B.txt 7 3 300 60 50

B.txt 8 4 400 70 20

Program code:

The code is as follows:

[chengmo@centos5 shell] $awk'

BEGIN {

If (ARGC

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report