ファイルの内容を並べ替えるために新しい行を挿入できますか?
私は持っています:
1:1 Lorem ipsum dolor sit amet consectetur 1:1 This is sample text of varying length.
adipiscing elit. 1:2 This is another paragraph in this file.
1:2 Vivamus integer non suscipit taciti mus Yet another sentence in this paragraph.
etiam at primis tempor sagittis. 1:3 Another paragraph can be found here!
数値がソートされるようにスペースを適切に追加するにはどうすればよいですか?
予想出力:
1:1 Lorem ipsum dolor sit amet consectetur This is sample text of varying length.
adipiscing elit.
1:2 Vivamus integer non suscipit taciti mus This is another paragraph in this file.
etiam at primis tempor sagittis. Yet another sentence in this paragraph.
1:3 Another paragraph can be found here!
編集:線が揃うので、数字を繰り返すことなく削除できます。
POSIXコンプライアンスを優先してください。
ベストアンサー1
GNU を使用して、awk
テキストを固定幅レコードセットとして読み込みます。各レコードは、幅が6(左側のラベル)、42(左側のテキスト行)、6(右側のラベル)、42(右側のテキスト行)のフィールドに分けられます。テキスト):
BEGIN {
FIELDWIDTHS = "6 42 6 42"
}
# New label seen on the left hand side.
# If this is a completely new label, then
# add it to the end of the "labels" array.
$1 != " " {
llabel = $1
if (!seenlabels[llabel]++)
labels[++n] = llabel
}
# Same as above, but for the right hand side.
$3 != " " {
rlabel = $3
if (!seenlabels[rlabel]++)
labels[++n] = rlabel
}
# Add text to the labelled paragraphs, left and right,
# as strings delimited by ORS (newline).
{
ltext[llabel] = (ltext[llabel] == "" ? $2 : ltext[llabel] ORS $2)
rtext[rlabel] = (rtext[rlabel] == "" ? $4 : rtext[rlabel] ORS $4)
}
# At end, output.
END {
# Iterate over all paragraphs (there are "n" of them).
for (i = 1; i <= n; ++i) {
delete llines
delete rlines
# Split the text for the left and right paragraph,
# into arrays, "llines" and "rlines".
a = split(ltext[labels[i]], llines, ORS)
b = split(rtext[labels[i]], rlines, ORS)
# The arrays may be of different lengths, but
# "c" will be the length of the longest, i.e.
# the number of lines of the paragraph to the
# left or right, whichever is longes.
c = (a > b ? a : b)
# Print the first line of the left and right
# of this paragarph (includes the label at the left).
printf("%-6s%-42s%-6s%-42s\n", labels[i], llines[1], "", rlines[1])
# Then print the other lines (no label).
for (j = 2; j <= c; ++j)
printf("%-6s%-42s%-6s%-42s\n", "", llines[j], "", rlines[j])
}
}
テスト:
$ cat file
1:1 Lorem ipsum dolor sit amet consectetur 1:1 This is sample text of varying length.
adipiscing elit. 1:2 This is another paragraph in this file.
1:2 Vivamus integer non suscipit taciti mus Yet another sentence in this paragraph.
etiam at primis tempor sagittis. 1:3 Another paragraph can be found here!
$ gawk -f script file
1:1 Lorem ipsum dolor sit amet consectetur This is sample text of varying length.
adipiscing elit.
1:2 Vivamus integer non suscipit taciti mus This is another paragraph in this file.
etiam at primis tempor sagittis. Yet another sentence in this paragraph.
1:3 Another paragraph can be found here!
awk
これは(variables)使用のためのPOSIX仕様へのGNU固有の拡張であるため、FIELDWIDTHS
厳密に言えばPOSIXの答えではありません。
POSIX準拠の回答を得るには、BEGIN
その部分を次のように置き換えます。
{
rec = $0
$0 = ""
$1 = substr(rec,1,6)
$2 = substr(rec,7,42)
$3 = substr(rec,49,6)
$4 = substr(rec,55)
}