带有正则表达式问题的 Linux CentOS sed 命令 [英] Linux CentOS sed command with regex issues

查看:50
本文介绍了带有正则表达式问题的 Linux CentOS sed 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CentOS 下有一个 txt 文件,我想将其中的任何 "\t\n" 替换为 "\t\t".我试过这个:

I have a txt file under CentOS in which I want to replace any "\t\n" with "\t\t". I tried this:

sed -i -E 's/\t\n/\t\t/g' myfile.txt

但它不起作用.不知道 CentOS 是否不支持 sed 中的正则表达式.

but it doesn't work. I don't know if CentOS doesn't support regex in sed.

感谢任何帮助!

附言

输入(两行):

1\t2\t3\t$4\t5\t6\t$

1\t2\t3\t$ 4\t5\t6\t$

输出(一行):1\t2\t\3\t\t4\t5\t6\t\t

Output(one line): 1\t2\t\3\t\t4\t5\t6\t\t

在 Editplus 中,查找正则表达式为 '\t\n',替换为 '\t\t'.那么所有以'\t\n'结尾的行都会变成一行,每个'\n'都被一个额外的'\t'替换.

In Editplus, the find regex is '\t\n' and the replace is '\t\t'. Then all lines ending with '\t\n' will become one line, and each '\n' is replaced by one additional '\t'.

附言

我的文件是这样读取的(cat -A myfile.txt)

my file is read like this (cat -A myfile.txt)


推荐答案

如果前一行有单个制表符,您可以使用这个 perl 命令来连接行:

You may use this perl command to join lines if previous line has a single tab:

perl -i -0777 -pe 's/(\S\t)\n(?!\z)/$1\t/g' excel.log

(?!\z) 是一个否定前瞻,使文件最后一行的匹配失败.

(?!\z) is a negative lookahead to fail this match for last line of the file.

这篇关于带有正则表达式问题的 Linux CentOS sed 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆