파일 내용을 정렬하기 위해 새 줄을 삽입할 수 있습니까?
나는 가지고있다:
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)
}