2017-04-04 44 views
0

使用sed的使用SED無法找到這種情況下,任何解決方案:合併入行沒有「空間」巨大的單行中的.txt文件

我有包含平均超過30〜行.txt文件。 我需要將所有行合併到一個沒有任何「空格」的每個文件(N個文件但少於1000個)的單個巨大行中,並將此行粘貼到一個新行(每個txt文件數據爲1行)。需要這種格式來解析.csv表。

實施例:

文件1:

This file 
Contains text 
to merge into 
a single line without any 
space between characters 

文件2:

This file 
Contains almost the same text 
to merge into 
a single line without any 
space or blanks between characters after the merge 

文件N:

This file 
Contains equal text 
to merge into 
a single line without any 
space between characters 

輸出:

This FileContains Textto Merge intoa single line without anyspace between characters 
This fileContains almost the same texto merge intoa single line without anyspace or blanks between characters after the merge 
This fileContains equal textto merge intoa single line without anyspace between characters 

試圖用:

sed -e :a -e '/$/N; s/\n//; ta' test.txt 

沒有工作。文本保持不變

樣本數據:

6150217008*AA*31/1*1 
0*4611*1 
349*9171*349*9171*0*0 
403*9481*403*9481*0*0 
C3*25* 
718*17399*718*17399*0*0 
C4*25* 
794*19293*794*19293*0*0 
C5*25* 
731*17594*731*17594*0*0 
C6*25* 

需要得到: 6150217008 * AA *1分之31* 10 * 4611 * 1349 * 9171 * 349 * 9171 * 0 * 0403 * 9481 * 403 * 9481 * 0 * 0C3 * 2 ....等

巨大的單一原始行文本沒有任何空格。

我該如何做到這一點?謝謝!

+0

'sed':1; s/^ [] * //; s/[] * $ //; $!{N; s/\ n //; B1}'' – 123

+0

6150217008 * VA * V1/1 * 1 DIX * DN 5800-4 * 7008 ** * EVS3.1 0 * 4611 * 1 865616 * 37165 * 865616 * 37165 * 0 * 0 等.. 幾乎刪除所有行,但不是全部 –

+0

也許你應該編輯你的Sample Data,因爲它沒有顯示任何空格,我想它應該包含一些。 – vdavid

回答

0

簡單TR命令的方法:

tr -d '[:space:]' < test.txt 

輸出:

6150217008*AA*31/1*10*4611*1349*9171*349*9171*0*0403*9481*403*9481*0*0C3*25*718*17399*718*17399*0*0C4*25*794*19293*794*19293*0*0C5*25*731*17594*731*17594*0*0C6*25* 

-d - 刪除字符

[:space:] - 使用perl

tr -d '[:space:]' <test.txt> output.txt 
+0

這將刪除所有空格。我認爲OP只想擺脫第一個和最後一個空白。 – vdavid

+0

作品在終端的輸出。如何在.txt/csv文件中以相同的格式保存輸出?太感謝了! –

+0

@vdavid,*一個巨大的單行原始文本,沒有任何**空格。* – RomanPerekhrest

0

很簡單:表示所有水平和垂直空白

https://linux.die.net/man/1/tr


要輸出重定向到一個新的文件中的字符的集合使用下面

perl -pe 's/^\s+|\s*$//g' your_file 
0

這是一個bash解決方案,他在字符串中的空白字符,但TRIMS領先和拖尾空白:

#! /bin/bash 

for fname in *.txt; do 
    while read -r line || [[ -n ${line} ]]; do 
     printf "%s" "${line}" 
    done < "${fname}" 
    printf "\n" 
# don't name the result file .txt if it's in the same dir or it'll be included 
done > result.dat 

假定此腳本在上班時間,文件的目錄中運行。您可以通過編輯for -loop

相關問題