큰 텍스트 파일을 한 줄의 문자열로 개혁합니다 (bash를 통해)
문제
파일 1 :
hello
- dictionary definitions:
hi
hello
hallo
greetings
salutations
no more hello for you
-
world
- dictionary definitions:
universe
everything
the globe
the biggest tree
planet
cess pool of organic life
-
이것 (거대한 단어 목록의 경우)을 용어 정의 형식 (용어 당 한 줄)으로 포맷해야합니다. 어떻게 이것을 달성 할 수 있습니까? 단어 중 어느 것도 동일하지 않으며 위에서 볼 수있는 구조만이 아닙니다. 결과 파일은 다음과 같습니다.
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you -
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life -
awk/sed/grep/cat은 일반적인 경쟁자입니다.
해결책
awk 'BEGIN {FS="\n"; RS="-\n"}{for(i=1;i<=NF;i++) printf("%s ",$i); if($1)print"-";}' dict.txt
출력 :
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you -
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life -
다른 팁
그리고 누가 Perl만이 우아하게 할 수 있다고 말합니까? :)
$ gawk -vRS="-\n" '{gsub(/\n/," ")}1' file
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life
또는
# gawk 'BEGIN{RS="-\n";FS="\n";OFS=" "}{$1=$1}1' file
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life
Perl One-Liner :
perl -pe 'chomp;s/^-$/\n/;print " "' File1
주어진
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life
이것은 당신의 필요한 출력입니다.
사용중인 스크립팅 언어는 확실하지 않으며 의사 코드는 다음과 같습니다.
for each line
if line is "-"
create new line
else
append separator to previous line
append line to previous line
end if
end for loop
이 하나의 라이너는 Theer가 항상 한 단어를 위해 6 줄이 될 조건에서 작동합니다.
sed 'N;N;N;N;N;N;N;N;s/\n/ /g' test_3
sed -ne'1{x;d};/^-$/{g;s/\n/ /g;p;n;x;d};H'
awk -v'RS=\n-\n' '{gsub(/\n/," ")}1'
제휴하지 않습니다 StackOverflow