Text-Processing

AWK - 列的列印範圍

  • June 19, 2020

如果我有以下格式的 csv 文件:

column1,column2,column3,column4,column5,column6,column7,column8

我只想awk列印我會使用的第 2 到第 7 列:

awk -F',' '{print $2 "," $3 "," $4 "," $5 "," $6 "," $7}' file.csv

並得到:

column2,column3,column4,column5,column6,column7

有沒有辦法連接第 2-7 列來簡化命令。當我考慮一個包含更多列的文件時,我的awk命令會變得非常長。

$ awk -v b=2 -v e=7 'BEGIN{FS=OFS=","} {for (i=b;i<=e;i++) printf "%s%s", $i, (i<e ? OFS : ORS)}' file
column2,column3,column4,column5,column6,column7

b=開始欄位編號,e=結束欄位編號。如果您需要處理帶引號欄位、嵌入逗號、換行符等的 CSV,請參閱https://stackoverflow.com/q/45420535/1745001

實用程序切割有一個緊湊的符號:

cut -d, -f2-7 <input-file>

生產:

column2,column3,column4,column5,column6,column7

回答@PlasmaBinturong 的評論:我的意圖是解決短呼叫序列的問題:“……我的 awk 命令會變得非常長……”。但是,您也可以找到按照您的需要排列欄位的程式碼。儘管我喜歡 awk、perl、python,但我經常發現建構一個特定的實用程序來擴展標準 *nix 的功能很有用。因此,這裡是測試腳本 s2 的摘錄,顯示了實用程序 recut 和arrange,兩者都允許重新排列和複製,arrange 還允許減小欄位範圍:

FILE=${1-data1}

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Input data file $FILE:"
head $FILE

pl " Results, cut:"
cut -d, -f2-7 $FILE

pl " Results, recut (modified as my-recut):"
my-recut -d "," 7,6,2-5 < $FILE

pl " Results, arrange:"
arrange -s "," -f 5,3-1,7,5,3-4,5 $FILE

從這些版本產生結果:

OS, ker|rel, machine: Linux, 3.16.0-10-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
cut (GNU coreutils) 8.23
recut - ( local: RepRev 1.1, ~/bin/recut, 2010-06-10 )
arrange (local) 1.15

-----
Input data file data1:
column1,column2,column3,column4,column5,column6,column7,column8

-----
Results, cut:
column2,column3,column4,column5,column6,column7

-----
Results, recut (modified as my-recut):
column7,column6,column2,column3,column4,column5

-----
Results, arrange:
column5,column3,column2,column1,column7,column5,column3,column4,column5

my-recut 是對 textutils 程式碼 recut 的輕微修改,arrange 是我們的擴展版。更多資訊:

recut   Process fields like cut, allow repetitions and re-ordering. (what)
Path    : ~/bin/recut
Version : - ( local: RepRev 1.1, ~/bin/recut, 2010-06-10 )
Length  : 56 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Home    : http://www1.cuni.cz/~obo/textutils/ (doc)
Modules : (for perl codes)
Getopt::Long   2.42

arrange Arrange fields, like cut, but in user-specified order. (what)
Path    : ~/bin/arrange
Version : 1.15
Length  : 355 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Modules : (for perl codes)
warnings       1.23
strict 1.08
Carp   1.3301
Getopt::Euclid 0.4.5

最良好的祝愿……乾杯,drl

引用自:https://unix.stackexchange.com/questions/532599