AWK - 列範囲印刷

AWK - 列範囲印刷

次の形式のcsvファイルがある場合:

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

列2から列7までだけ印刷したいと思いますawk。以下を使用します。

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

そして得る:

column2,column3,column4,column5,column6,column7

コマンドを単純化するために列2-7を接続する方法はありますか?より多くの列を持つファイルを考慮すると、コマンドawkは非常に長くなります。

答え1

実用的なチラシには、以下の簡潔な表記法があります。

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

生産:

2列、3列、4列、5列、6列、7列

@PlasmaBinturongのコメントに答えるには:私の意図は短い呼び出しシーケンスの問題を解決することでした。 「…私​​のawkコマンドは非常に長くなるでしょう…」ただし、必要に応じてフィールドを並べ替えるコードを見つけることもできます。私はawk、Perl、Pythonが好きですが、標準* nixの機能を拡張する特定のユーティリティを構築するのが便利であることがよくあります。以下は、ユーティリティを切り捨ててソートするテストスクリプトs2からの抜粋です。どちらも再配置とコピーを可能にし、並べ替えによってフィールドの範囲を減らすこともできます。

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をわずかに変更しますが、alignmentは拡張カットバージョンです。追加情報:

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

答え2

$ 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

答え3

sed -e '
  s/,/\n/7        ;# tag the end of col7
  s/^/,/          ;# add a comma
  s/,/\n/2        ;# tag beginning of col2
  s/.*\n\(.*\)\n.*/\1/ ;# perform surgery 
' file.csv

結果:

column2,column3,column4,column5,column6,column7

答え4

これは私にとって効果的です。

awk '{ for (i=2; i<=7;i++){ printf $i; if (i != 7){ printf "," }} print "" }'

少数の列については(コマンドの複雑さの観点から)意味がありません。ただし、このアプローチは入力に熱が多い場合に便利です。

関連情報