Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何从文件[shell]中某些特定行的结尾\n删除?_Linux_Bash_Shell_Unix_Redhat - Fatal编程技术网

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

Linux 如何从文件[shell]中某些特定行的结尾\n删除?,linux,bash,shell,unix,redhat,Linux,Bash,Shell,Unix,Redhat,在Redhat中,我的file.csv包含以下数据: 170033101;20170302;;;"Free text 1" 170033101;20170302;;;"Free text 2" 170033101;20170302;;;"Free text 3" 170033101;20170302;;;"Free text 4" 在从文件中删除错误的\n后,我想创建另一个已更正的文件(Correct_file.csv),如下所示: 170033101;20170302;;;"Free te

在Redhat中,我的file.csv包含以下数据:

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free 
text 3"
170033101;20170302;;;"Free text 4"
在从文件中删除错误的\n后,我想创建另一个已更正的文件(Correct_file.csv),如下所示:

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free text 3"
170033101;20170302;;;"Free text 4"
我的解决方案:

我制作了下面的shell脚本来查找那些不以170开头的行之前的行,然后创建sed.txt,该sed.txt为每个错误的行都有一个sed行,用空格替换\n

我无法根据行号执行sed命令或tr命令删除特定行

我的剧本:

>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

我调用脚本并传递2个参数1-旧文件2-新更正的文件,新文件与旧文件完全相同,没有任何更正。

sed
返回新字符串,因此不需要
回送它。只需将其称为
sed..>>data.txt

下面的sed语句将用零替换行末尾的新行。您只需要传递要翻译的行

sed ':a;N;$!ba;s/\n//g' <LINE INPUT>
sed:a;N、 美元!文学士;s/\n//g'

如果您向它传递一个文件,它将以循环方式读取整个文件,并用空格替换换行符。

sed
返回新字符串,因此您不需要
回送它。只需将其称为
sed..>>data.txt

下面的sed语句将用零替换行末尾的新行。您只需要传递要翻译的行

sed ':a;N;$!ba;s/\n//g' <LINE INPUT>
sed:a;N、 美元!文学士;s/\n//g'

如果您向其传递一个文件,它将以循环方式读取整个文件,并用空格替换换行符。

您可以使用此
sed

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

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


您可以使用此
sed

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

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


当我想使用
\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"
此perl oneliner使用单个空格替换每一行
\n
,后面不带引号


PS:您可以在命令末尾添加
>newfile
,将“更正的”输出发送到
newfile
,或者您甚至可以使用
-i
perl开关就地编辑当前文件。

当我想使用
\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"
此perl oneliner使用单个空格替换每一行
\n
,后面不带引号


PS:您可以在命令末尾添加
>newfile
,将“更正的”输出发送到
newfile
,或者您甚至可以使用
-i
perl开关在位编辑当前文件。

您可以使用此
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"

您可以使用此
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"

试着跟着awk走一次

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

在这里检查是否有任何行,如果不是从数字开始,则通过RS(记录分隔符)打印新行,确保它不会发生在第一行上,否则不打印任何内容。在awk printing NULL的结尾部分,最后将打印一行新行。

也尝试一次跟随awk

awk '{printf("%s%s",$0 !~ /^[0-9]+/?"":(NR>1?RS:""),$0)} END{print ""}'  Input_file
在这里检查是否有任何行,如果不是从数字开始,则通过RS(记录分隔符)打印新行,确保它不会发生在第一行上,否则不打印任何内容。在awk打印的最后一段,将打印一个新行