[英]How to delete newline and character at the start of line using Bash?
I need to delete a newline with exact character at the beginning of the line.我需要在行的开头删除一个带有确切字符的换行符。
For example, I have new.txt file which contains the following lines:例如,我有 new.txt 文件,其中包含以下几行:
"AAA"AAA
"BBB
"""BBB
"CCC"CCC
Expected result is:预期结果是:
"AAA"AAA
"BBB"BBB
"CCC"CCC
I tried using sed
but it is not working.我尝试使用
sed
但它不起作用。
sed -i -e 's/\n"""/"/g' new.txt
Please help me to solve this.请帮我解决这个问题。
With Perl.使用 Perl。
perl -i -0777pe 's/\n"""/"/' new.txt
Output to new.txt
:输出到
new.txt
:
"AAA"AAA "BBB"BBB "CCC"CCC
Use tr to replace the \\r\\n .使用tr替换\\r\\n 。 Use the sed command and then replace the \\r\\n back.
使用sed命令,然后将\\r\\n替换回。 \\r is there since your file has Windows line endings.
\\r存在,因为您的文件具有 Windows 行尾。
tr '\r' '\t' < new.txt | tr '\n' '\f' | sed -e 's/\t\f"""/"/' | tr '\f' '\n' | tr '\t' '\r'
This might work for you (GNU sed):这可能对你有用(GNU sed):
sed 'N;s/\n"""/"/;P;D' file
Append the next line to the current line.将下一行追加到当前行。 If the pattern space now contains
\\n"""
replace it by "
and then print/delete the first line in the pattern space and repeat.如果模式空间现在包含
\\n"""
将其替换为"
然后打印/删除模式空间中的第一行并重复。
With GNU sed:使用 GNU sed:
$ cat new.txt
"AAA"AAA
"BBB
"""BBB
"CCC"CCC
$ sed ':x;N;s/\n""//;bx' new.txt
"AAA"AAA
"BBB"BBB
"CCC"CCC
GNU - sed manual - joining lines GNU - sed 手册 - 加入行
T. T。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.