私はbashスクリプトとファイル内のテキストでbashとawkに最初に触れました。
"Index", "Year", "Age", "Name", "Movie"
1, 1928, 44, "Emil Jannings", "The Last Command, The Way of All Flesh"
2, 1929, 41, "Warner Baxter", "In Old Arizona"
3, 1930, 62, "George Arliss", "Disraeli"
4, 1931, 53, "Lionel Barrymore", "A Free Soul"
この出力を取得し、俳優名で並べ替え、タイトル名を変更し、テーブルを含む関数を作成する必要があります。
Actor Year Age Film
Emil Jannings 1928 44 The Last Command, The Way of All Flesh
George Arliss 1930 62 Disraeli
Lionel Barrymore 1931 53 A Free Soul
Warner Baxter 1929 41 In Old Arizona
あなたは何をしますか?私はまだ初心者であり、欲しいものを得るための正しい方法を見つけることができません。
ありがとう
答え1
csvcut
PythonベースのcsvkitとMillerから:
$ csvcut -S -c Name,Year,Age,Movie file.csv |
mlr --icsv --opprint sort -f Name then rename Name,Actor,Movie,Film
Actor Year Age Film
Emil Jannings 1928 44 The Last Command, The Way of All Flesh
George Arliss 1930 62 Disraeli
Lionel Barrymore 1931 53 A Free Soul
Warner Baxter 1929 41 In Old Arizona
Millerがこれを自分で実行できるはずだと思いますが、単一文字ではない場合は、引用符付き区切り文字を誤って解析するようです。
答え2
FPATでGNU awkを使用する:
$ cat tst.awk
BEGIN {
in2outTag["Name"] = "Actor"
in2outTag["Movie"] = "Film"
numOutFlds = split("Actor Year Age Film",outTags)
FPAT = "\\s*(([^,]*)|(\"[^\"]+\"))\\s*"
OFS = "\t"
}
{
for (inFldNr=1; inFldNr<=NF; inFldNr++) {
gsub(/^[[:space:]]*"?|"?[[:space:]]*$/,"",$inFldNr)
}
}
NR == 1 {
for (inFldNr=1; inFldNr<=NF; inFldNr++) {
tag = ( $inFldNr in in2outTag ? in2outTag[$inFldNr] : $inFldNr )
tag2inNr[tag] = inFldNr
}
printf "%d%s", 0, OFS
for (outFldNr=1; outFldNr<=numOutFlds; outFldNr++) {
tag = outTags[outFldNr]
out2inNr[outFldNr] = tag2inNr[tag]
printf "%s%s", tag, (outFldNr < numOutFlds ? OFS : ORS)
}
next
}
{
printf "%d%s", 1, OFS
for (outFldNr=1; outFldNr<=numOutFlds; outFldNr++) {
inFldNr = out2inNr[outFldNr]
val = $inFldNr
printf "%s%s", val, (outFldNr < numOutFlds ? OFS : ORS)
}
}
$ awk -f tst.awk file | sort -t$'\t' -k1,1n -k2,2 | cut -f2-
Actor Year Age Film
Emil Jannings 1928 44 The Last Command, The Way of All Flesh
George Arliss 1930 62 Disraeli
Lionel Barrymore 1931 53 A Free Soul
Warner Baxter 1929 41 In Old Arizona