使用 Sed、awk 或 tr,如何在特定字符后添加换行符?
With Sed, awk or tr, how to add a newline after a specific character?
我有这个文件:
lol.txt:hungrylol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungrylol6.txt:hungry
我想变成这个:
lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry
我已经用 sed、tr 和 awk 进行了调查,但没有找到一种方便的方法。
谢谢
尝试:
awk '{gsub(/hungrylol/,"hungry\nlol");print}' Input_file
我在全球范围内将字符串 hungrylol 替换为 hungry 然后换行和 lol 然后打印 Input_file.
行
如果没有"hungry"就在"hungry"之后添加一个新行:
$ sed -r 's/(hungry)([^\n])/\n/g' file
lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry
这匹配 hungry
后跟一个不是新行的字符。发生这种情况时,它会将其替换为 hungry
+ 新行 + 捕获的字符。
使用 BRE 的 sed:
sed 's/\(hungry\)\(.\)/\n/' file
当hungry
后跟一个字符时,换行和捕获的字符输出。
我有这个文件:
lol.txt:hungrylol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungrylol6.txt:hungry
我想变成这个:
lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry
我已经用 sed、tr 和 awk 进行了调查,但没有找到一种方便的方法。
谢谢
尝试:
awk '{gsub(/hungrylol/,"hungry\nlol");print}' Input_file
我在全球范围内将字符串 hungrylol 替换为 hungry 然后换行和 lol 然后打印 Input_file.
行如果没有"hungry"就在"hungry"之后添加一个新行:
$ sed -r 's/(hungry)([^\n])/\n/g' file
lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry
这匹配 hungry
后跟一个不是新行的字符。发生这种情况时,它会将其替换为 hungry
+ 新行 + 捕获的字符。
使用 BRE 的 sed:
sed 's/\(hungry\)\(.\)/\n/' file
当hungry
后跟一个字符时,换行和捕获的字符输出。