Split joy ajratilgan elementlarni alohida qatorlarga bo'linadi

Quyidagi kabi bo'shliqlar bilan ajralib turadigan predmetlar bo'lmagan fayllar mavjud bo'lsa:

ABC123
ABC124
ABC125  ABC321  ABC222  ABC111  ABC333
ABC069  ABC450  ABC595

Ularning har birini alohida satrda qanday yozish mumkin? (python yoki grep/awk va boshqalar)

1
Iltimos, urinishlaringizni ko'rsata olasizmi?
qo'shib qo'ydi muallif Christian Dean, manba
@ user1658415: Bu sizning so'zlaringiz orasida faqat bo'shliqlar ni emas, balki tabs ni ham ko'rsatadi; Bunday holatda tr -s '[: blank:]' '\ n' ishlashi kerak.
qo'shib qo'ydi muallif mklement0, manba
awk '{for (i = 1; i <= NF; i ++) bosma $ i}' fayl men uchun ishlagan. Men oldin tr -s '' '\ n'
qo'shib qo'ydi muallif user1658415, manba

6 javoblar

Buni ko'ring:

Agar sizda faqat bo'sh joylar mavjud bo'lsa:

tr -s ' ' '\n' < file

Agar bo'shliqlar va yYoKIliqlar mavjud bo'lsa, @ mklement0 tomonidan taklif qilinganidek:

tr -s '[:blank:]' '\n' < file

YoKI

awk '{fYoKI(i=1;i<=NF;i++) print $i}' file

YoKI

egrep -o 'ABC[[:digit:]]{3}' file

YoKI

egrep -o '[[:alnum:]]{6}' file
2
qo'shib qo'ydi
Rahmat! Men uchun ishlaydigan awk '{for (i = 1; i <= NF; i ++) $ i} faylini chop etdi. Ilgari muvaffaqiyatsiz tr -s '' '\ n' ni sinab ko'rdim. Sizga katta rahmat!
qo'shib qo'ydi muallif user1658415, manba

sed command approach:

sed -E 's/\s+/\n/g' testfile

-E option, allows extended regular expressions


awk approach:

awk '{gsub(/ +/, "\n", $0); print}' testfile

gsub(regexp, replacement [, target])
      Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and       replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace       everywhere.


grep approach:

grep -o '\S*' testfile

\S* - matches all non-whitespace sequences

1
qo'shib qo'ydi
Yaxshi echimlar; sed va grep buyruqlaringiz faqat GNU ilovalari bilan ishlayotganligini ta'kidlash kerak.
qo'shib qo'ydi muallif mklement0, manba

Siz xargs dan foydalanishingiz mumkin:

xargs -n 1 < file

Chiqish:

ABC123
ABC124
ABC125
ABC321
ABC222
ABC111
ABC333
ABC069
ABC450
ABC595
0
qo'shib qo'ydi
Bu jozibasi bilan qisqa bo'lsa-da, har bir kirdi argumenti uchun /bin/echo bola jarayonini yaratadi, bu juda samarasiz.
qo'shib qo'ydi muallif mklement0, manba

Eslatma: Quyidagi barcha echimlar butun faylni xotiraga tiklashni oldini olish va satr ichki ichki oynalar bilan bir qatorda bo'shliqlar bilan ishlamaslik uchun satrini qatoriga kiritadi.

VIPIN KUMAR ning foydali javoblaridan standart UNIX yordam dasturlarini ishlatib, POSIX-mos uz echimini samarali va oddiy bo'lsa, uning POSIX-mos keluvchi awk echimi ham yaxshi ishlaydi; oddiy bo'lmagan POSIX-mos keluvchi hal bo'lishi mumkin - pastga qarang.

yorlig'i belgilarini ham ko'rib chiqadigan tr komandasining umumlashtirilgan versiyasi:

tr -s '[:blank:]' '\n' < file

GNU awk yoki Mawk (faqat BSD/macOS awk ) dan foydalanish:

awk -v RS='[[:space:]]+' '1' file

grep (GNU va UX/macOS grep ) bilan ishlash:

grep -Eo '\S+' file

Python (har ikki v2 va v3) dan foydalanish:

Shell bir-liner ( bash , $ '...' dan foydalanish tufayli):

python -c $'import fileinput\nfor line in fileinput.input(): print("\\n".join(line.split()))' file

Skriptda:

for line in open('file'):
  print("\n".join(line.split()))

ruby dan foydalanish:

Shell bir-liner:

ruby -ne 'puts $_.split' file

Skriptda:

File.open('file').each_line do |line|
  puts line.split
end
0
qo'shib qo'ydi

Splitni ishlatish:

a = '''ABC123
ABC124
ABC125  ABC321  ABC222  ABC111  ABC333
ABC069  ABC450  ABC595'''

for item in a.split():
    print(item)

beradi:

ABC123
ABC124
ABC125
ABC321
ABC222
ABC111
ABC333
ABC069
ABC450
ABC595
0
qo'shib qo'ydi
file = open("testfile.txt", "r")
for line in file:
    newLine = line
    newLine = newLine.replace("\n","")
    newLine = newLine.split(" ")
    for eachElement in newLine:
        if eachElement!="" and len(eachElement)!=0:
            print eachElement
0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming