إعادة ملف نصي كبير إلى سلاسل سطر واحد (عبر BASH)

StackOverflow https://stackoverflow.com/questions/1617632

  •  06-07-2019
  •  | 
  •  

سؤال

و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
-

ولست بحاجة لتنسيق هذا (للحصول على قائمة ضخمة من الكلمات) في المدى إلى تنسيق الوضوح (سطر واحد في الفصل الدراسي الواحد). كيف يمكن للمرء أن تحقيق ذلك؟ أي من الكلمات هي نفسها، إلا أن هيكل رأينا أعلاه هو. ان الملف الناتجة ننظر بشيء من هذا القبيل:

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 / سد / البقرى / القط هي المتنافسين المعتاد.

هل كانت مفيدة؟

المحلول

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   -

نصائح أخرى

ووالذي يقول فقط بيرل تستطيع ان تفعل ذلك بأناقة؟ :)

$ 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

وOR

# 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

وA بيرل أونيلينير:

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'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top