[英]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.