pats4u
pats4u

Reputation: 271

Replace first word of a delimited file

I need to replace the first word of first line of the file .
Ex: In the below '^' delimited file, i need to replace HEADER1 with PARA1.

cat FILE.TXT  
HEADER1^HEADER2^HEADER3^HEADER4^HEADER5^HEADER6  
SUBHEADER1^SUBHEADER2^SUBHEADER3^SUBHEADER4^SUBHEADER5^SUBHEADER6^SUBHEADER7^SUBHEADER8^SUBHEADER9^SUBHEADER10^SUBHEADER11^SUBHEADER12^SUBHEADER13  
10^56^Q1^Q1^95^02^ ^ ^4^20170501^20170501^ABCD^  
20^56^Q2^Q2^95^02^ ^ ^4^20170502^20170502^ABCD^  
30^56^Q3^Q3^95^02^ ^ ^4^20170503^20170503^ABCD^  

I do not want to use sed to find the first occurence of HEADER1 in the file & replace it.
Instead it should work for any value in first word of the first line of file .

Any suggestions are highly appreciated.

Thanks.

Upvotes: 1

Views: 1132

Answers (1)

W.Mann
W.Mann

Reputation: 923

If I understand you correctly, you don't want to use sed because you think it limits you to replace a specific string by another string? sed expects regular expressions, which allow you to do the following:

sed -e '1 s/^[^^]*/PARA1/' < /tmp/input.txt

This replaces the first word of input (on the first line only) by PARA1, assuming that the words are separated by ^.

Upvotes: 2

Related Questions