Linux CentOS sed command with regex issues

Viewed 133

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

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

Any help is appreciated!

p.s.

Input(two lines):

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

Output(one line): 1\t2\t\3\t\t4\t5\t6\t\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'.

p.s.

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

enter image description here


2 Answers

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) is a negative lookahead to fail this match for last line of the file.

You need to escape the backslashes.

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