2017-04-04 45 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

相关问题