Replace First Column and First Row Data

HI Guys,

I just want to replace data for First Column and Row Cell(1,1)

Input :-

Hello	A	B	C
X	1	2	3
Y	4	5	6
Z	7	8	9

Output:-

Byee	A	B	C
X	1	2	3
Y	4	5	6
Z	7	8	9

From Hello to Byee .....And The Each file have Different String.

Hello pareshkp,

Could you please try following and let me know if this helps.

awk '(NR==1 && $1=="Hello"){sub("Hello","Byee",$0);} 1' Input_file

Output will be as follows.

Byee A B  C
X 1 2 3
Y 4 5 6
Z 7 8 9

Thanks,
R. Singh

1 Like

Thanks ... But the Hello String will change in all files.
Do we have any other idea

---------- Post updated at 09:43 AM ---------- Previous update was at 09:36 AM ----------

Got it Thanks

nawk '(NR==1){sub($1,"Byee",$0);} 1'

Hello PareshKP,

I don't think so code posted in POST#2 will substitute all the strings Hello , in a single file, here is an example for same.

cat Input_file
Hello A B C
X 1 2 3
Hello 4 5 6
Z 7 8 9

Then same code as POST#2 following will be the output.

awk '(NR==1 && $1=="Hello"){sub("Hello","Byee",$0);} 1'  Input_file
Byee A B  C
X 1 2 3
Hello 4 5 6
Z 7 8 9

Please do let us know if your requirement if a little different, in my code only it is looking for string Hello if you want to change any thing in $1 with Byee then your code is correct too except one change, where if you have multiple file then following may help you in same. Change NR with FNR as FNR variable will be RESET whenever there is new file is being read, on the other hand NR 's value will be keep increasing till the last file read.

awk '(FNR==1){sub($1,"Byee",$0);} 1'  Input_file Input_file1

Thanks,
R. Singh

1 Like