简体   繁体   English

Sed和awk导致线环绕

[英]Sed and awk causing line wrap-around

I have a file of the form: 我有一个表格的文件:

FA01_01:The birch canoe slid on the smooth planks  
FA01_02:Glue the sheet to the dark blue background

I need it to be of the form (also note the use of lowercase): 我需要它的形式(也注意使用小写):

<s> the birch canoe slid on the smooth planks </s> (FA01_01)  
<s> glue the sheet to the dark blue background </s> (FA01_02)

so I tried the following expression with sed: 所以我用sed尝试了以下表达式:

sed 's/\(.......\):\(.*$\)/(\1) <s> \2 <\/s>/' tmp.dat

but this is what it returned: 但这是它返回的内容:

</s> (FA01_01)anoe slid on the smooth planks  
</s> (FA01_02)eet to the dark blue background

For whatever reason, it appears that sed is causing the replaced pattern to wrap around to the beginning of the line but only for the second match. 无论出于何种原因,似乎sed导致被替换的模式环绕到行的开头但仅用于第二个匹配。 Example: 例:

$> sed 's/\(.......\):\(.*$\)/\1 \2/' tmp.dat
FA01_01 The birch canoe slid on the smooth planks

Is correct, but 是的,但是

$>sed 's/\(.......\):\(.*$\)/\2 \1/' tmp.dat
FA01_01h canoe slid on the smooth planks

This even occurs with awk as well. 这甚至也出现在awk中。 For sake of testing the wraparound hypothesis: 为了测试环绕假设:

$> awk 'BEGIN{FS=":"}{print tolower($2) "XXX"}' tmp.dat
XXX birch canoe slid on the smooth planks

but

$> awk 'BEGIN{FS=":"}{print tolower($1) "XXX"}' tmp.dat
fa01_01XXX

Any ideas what would be causing this line wrap? 什么会导致这个换行? Does it have anything to do with the fact that the second pattern or saved column goes until the end of line? 是否与第二个模式或已保存的列一直到行尾的事实有关?

The reason is that your tmp.dat is probably in DOS format (with \\r characters). 原因是你的tmp.dat可能是DOS格式(带\\ r字符)。 You could try to convert it to linux format (with only \\n), for example with the command: 您可以尝试将其转换为linux格式(仅使用\\ n),例如使用以下命令:

dos2unix tmp.dat

And then execute: 然后执行:

sed 's/\(.......\):\(.*$\)/<s>\L \2 \E<\/s> (\1)/' tmp.dat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM