Sed command to replace a word in a tab and space-separated line of text

Viewed 147

I have the below fields separated by tabs and spaces in file "text.txt". I want to use sed command to find "^@\t*\stext1\t\stext2\t\s100" and replace it with "^@\t\stext1\t\stext2\t\s*1000"

<Field1>     <Field2>       <Field3>    <Field4>
@             text1         text2        100
$             text3         text4        200

I have tried using the below sed command:

sed -i "/^\s*\@\s+text1\s+text2\s*/c\@/\t/\ttext1/\ttext2/\t/\t1000" /text.txt

However, nothing is getting replaced in the file.

1 Answers

Your main issue is that you are using unescaped + in the POSIX BRE regex that is treated as a literal + symbol.

You need to use -E option to enable POSIX ERE syntax, where + is treated as a quantifier. Besides, you have several redundant / chars in the replacement, you need to remove them.

You can use

sed -E -i "/^\s*\@\s+text1\s+text2\s*/c\@\t\ttext1\ttext2\t1000" file

See the online demo.

Related