简体   繁体   English

如何从文件[shell]中某些特定行的末尾删除\\ n?

[英]How to delete \n from the end of some particular lines in file [shell]?

In Redhat I have file.csv that has below data: 在Redhat中,我具有以下数据的file.csv:

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free 
text 3"
170033101;20170302;;;"Free text 4"

I want to create another corrected file (Correct_file.csv) after removing the wrong \\n from the file to be as below: 我要从文件中删除错误的\\ n后创建另一个更正的文件(Correct_file.csv),如下所示:

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free text 3"
170033101;20170302;;;"Free text 4"

My solution: 我的解决方案:

I made the below shell script to find rows preceding those lines that don't start with 170, and then create sed.txt that has a sed line for each wrong row to replace the \\n with space. 我在下面的shell脚本中查找了不在以170开头的行之前的行,然后创建了sed.txt,其中每个错误行都有sed行,以用空格替换\\ n。

I am unable to perform the sed command or tr command to remove a specific line based on the line number 我无法执行sed命令或tr命令来删除基于行号的特定行

My Script: 我的剧本:

>sed.txt;
for i in `grep -nv '^[1706]' $1|cut -f 1 -d \:`
do
if [ $i -eq 1 ]
then
continue
else
j=`expr $i - 1`
echo $j"s/\n//" >>sed.txt
fi
done
sed -f sed.txt $1 >$2

I call the script and pass 2 parameters 1- old file 2- new corrected file, and the new file is exactly as the old with no correction. 我调用脚本并传递2个参数1-旧文件2-新的更正文件,新文件与没有更正的旧文件完全相同。

You can use this awk command that works bases on the fact if a line ends with " or not: 您可以使用以下awk命令,该命令基于以下事实:行是否以"结尾:

awk '!/"$/{p=$0; next} p!=""{$0 = p $0; p=""} 1' file

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free text 3"
170033101;20170302;;;"Free text 4"

sed returns the new string, so you don't need to echo it. sed返回新字符串,因此您无需echo它。 Simply call it sed .. >> data.txt 只需将其称为sed .. >> data.txt

The following sed statement will replace the new line at the end of a line with nothing. 以下sed语句将用新行替换掉新行。 You need to pass only the lines you want to translate 您只需要传递要翻译的行

sed ':a;N;$!ba;s/\n//g' <LINE INPUT>

If you pass it a file, it will read the whole file in a loop, and replace the newline(s) with a space. 如果将文件传递给它,它将循环读取整个文件,并用空格替换换行符。

You can use this sed : 您可以使用此sed

sed '/^170/{:loop; N;/\n170/{P;D;t}; s/\n//g;b loop}' file

Input: 输入:

$ cat file
170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free 
text 3"
170033101;20170302;;;"Free 
text 
4"

Test: 测试:

$ sed '/^170/{:loop; N;/\n170/{P;D;t}; s/\n//g;b loop}' file > correct_file.csv
170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free text 3"
170033101;20170302;;;"Free text 4"

When i want to work with \\n i prefer simple perl over sed: 当我想使用\\n我更喜欢简单的perl而不是sed:

$ cat file1
170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free
text 3"
170033101;20170302;;;"Free text 4" 

$ perl -pe 's/[^"]\n/ /g' file1
170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free text 3"
170033101;20170302;;;"Free text 4"

This perl oneliner replaces with a single space every new line \\n not followed by quotes " 该perl oneliner会在每行新行中用一个空格替换\\n而不用引号引起来"

PS: You can add >newfile at the end of the command to send the "corrected" output to a newfile or you can even edit the current file in place using -i perl switch. PS:您可以添加>newfile在命令结束发送“纠正”输出到newfile ,也可以代替使用甚至编辑当前文件-i perl的开关。

try following awk too once. 尝试跟随awk一次。

awk '{printf("%s%s",$0 !~ /^[0-9]+/?"":(NR>1?RS:""),$0)} END{print ""}'  Input_file

Checking here if any line if not starting from digits then printing new line there by RS(record separator) making sure it shouldn't happen on very first line else printing nothing. 检查此处是否有任何不是从数字开始的行,然后通过RS(记录分隔符)在此处打印新行,以确保它不应该出现在第一行,否则什么也不打印。 in END section of awk printing NULL which will print a new line at last. 在awk的END部分中打印NULL,最后将打印新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Shell - 如何从具有多种模式的文本文件中删除行? - Shell - How to delete lines from text file with multiple patterns? 如何从文件中提取特定行并将其附加到Shell脚本中的另一个现有文件中,然后从原始文件中删除? - how to extract specific lines from file and append it to another existing file in shell script and then delete from original? 从 shell 中的文件中删除模式行的快速方法 - Fast way to delete pattern lines from a file in shell 阅读后如何从文件中删除行? - How to delete lines from file after reading it? 如何用sed \\ awk从文件中删除行? - How to delete lines from file with sed\awk? 如何使用bash删除文件中的前N行并保留其余行 - How to delete first N lines in a file and retain the remaining lines using bash 从与脚本中第二个文件中的前两个字段匹配的文件中删除行 - Delete lines from a file matching first 2 fields from a second file in shell script 如何跳过两行并删除两行,直到vim中的文件结束? - how to skip two lines and delete two lines till the end of file in vim? 当我在shell脚本中使用cat cat命令时,如何在文件末尾保留空行 - How to keep blank lines in the end of a file when I user cat command in shell script 如何在bash shell脚本中从文件中添加两行数字? - How to add two lines of number from a file in bash shell scripting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM