sedを使用して次の入力文字列を出力文字列にフォーマットするにはどうすればよいですか?
中央の文字列は20番目の文字で始まり、最後の文字列は40番目の文字で始まる必要があります。
入力する:
begining center end
beg12 cen12 end12
beg13 cen end
出力:
begining center end
beg12 cen12 end12
beg13 cen end
ベストアンサー1
awk
その機能を使用してこのデータをフォーマットすることができますprintf()
。
$ awk '{ printf("%-20s%-20s%s\n", $1, $2, $3) }' data.in
begining center end
beg12 cen12 end12
beg13 cen end
これは、ファイルのデータがスペースで区切られていると仮定します。
列の幅をパラメータとして指定します。
$ cols=40
$ awk -v c="$cols" 'BEGIN { fmt=sprintf("%%-%ds%%-%ds%%s\n", c, c) } { printf(fmt, $1, $2, $3) }' data.in
begining center end
beg12 cen12 end12
beg13 cen end