Linux 替换换行符
我有一个XML文件,其中偶尔有几行被分成2行:第一行以Linux 替换换行符,linux,sed,newline,Linux,Sed,Newline,我有一个XML文件,其中偶尔有几行被分成2行:第一行以和#13结尾。我想连接任何这样的行并删除 ,可能将其替换为空格 e、 g 没有运气 非常感谢您的帮助 您可以使用此awk: awk -F" " '/ $/ {a=$1; next} a{print a, $0; a=""; next} 1' file 解释 -F”和#13;设置和#13作为分隔符,因此第一个字段将始终是字符串的所需部分 / ;$/{a=$1;next}如果行以和#13结尾,将其存
和#13结尾代码>。我想连接任何这样的行并删除
代码>,可能将其替换为空格
e、 g
没有运气
非常感谢您的帮助 您可以使用此awk:
awk -F" " '/ $/ {a=$1; next} a{print a, $0; a=""; next} 1' file
解释
-F”和#13;
设置和#13
作为分隔符,因此第一个字段将始终是字符串的所需部分
/
;$/{a=$1;next}
如果行以和#13结尾
,将其存储在a
中并跳到下一行
a{print a,$0;a=“”;next}
如果设置了a
,则将其与当前行一起打印。然后为将来的循环取消设置a
。最后跳到下一行
1
为真时,打印当前行
样品
$cat a
是 啊
嗨,我是
;
在这里
你好
再见
$awk-F“
;”/
;$/{a=$1;next}a{print a,$0;a=“”;next}1'a
是 啊
嗨,我在这里
你好
再见
您可以使用此awk:
awk -F" " '/ $/ {a=$1; next} a{print a, $0; a=""; next} 1' file
解释
-F”和#13;
设置和#13
作为分隔符,因此第一个字段将始终是字符串的所需部分
/
;$/{a=$1;next}
如果行以和#13结尾
,将其存储在a
中并跳到下一行
a{print a,$0;a=“”;next}
如果设置了a
,则将其与当前行一起打印。然后为将来的循环取消设置a
。最后跳到下一行
1
为真时,打印当前行
样品
$cat a
是 啊
嗨,我是
;
在这里
你好
再见
$awk-F“
;”/
;$/{a=$1;next}a{print a,$0;a=“”;next}1'a
是 啊
嗨,我在这里
你好
再见
尝试一下这种笨拙的行为:
awk -v RS="" 'gsub(/ \n/," ")+7' file
这里用您的示例进行测试:
kent$ echo "<message>hi I am
here </message>"|awk -v RS="" 'gsub(/ \n/," ")+7'
<message>hi I am here </message>
kent$echo“你好,我是
;
此处“|awk-v RS=”“”gsub(/
;\n/,”)+7”
嗨,我在这里
尝试一下这种笨拙的行为:
awk -v RS="" 'gsub(/ \n/," ")+7' file
这里用您的示例进行测试:
kent$ echo "<message>hi I am
here </message>"|awk -v RS="" 'gsub(/ \n/," ")+7'
<message>hi I am here </message>
kent$echo“你好,我是
;
此处“|awk-v RS=”“”gsub(/
;\n/,”)+7”
嗨,我在这里
这将适用于您:
sed -i '{:q;N;s/&.*\n/ /g;t q}' <filename>
-s
来自手册页:
-s, --squeeze-repeats
replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character.
这将对您有用:
sed -i '{:q;N;s/&.*\n/ /g;t q}' <filename>
-s
来自手册页:
-s, --squeeze-repeats
replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character.
以下是GNUsed
版本:
sed ':a;$bc;N;ba;:c;s/ \n/ /g' file
说明:
以下是GNUsed
版本:
sed ':a;$bc;N;ba;:c;s/ \n/ /g' file
说明:
因此,正确地将其作为一个相关问题提出:正确地将其作为一个相关问题提出:在我对我的文件运行dos2unix之后,这就起到了作用。谢谢大家的帮助。在我对文件运行dos2unix之后,这一切都起到了作用。谢谢大家的帮助。
sed '
:a # Create a label a
$bc # If end of file then branch to label c
N # Append the next line to pattern space
ba # branch back to label a to repeat until end of file
:c # Another label c
s/ \n/ /g # When end of file is reached perform this substitution
' file