yet another sed/awk question

Unix Guru's ,

I have a file all_files.txt containing data as follows

all_files.txt

first  file : /a/b/c/file.sh  first second CLIENT1
second file : /a/b/c/file.sh  first second CLIENT1

first file : /a/b/c/file.sh  first second CLIENT2
second file : /a/b/c/file.sh  first second CLIENT2

first file : /a/b/c/file.sh  first second CLIENT3
second file : /a/b/c/file.sh  first second CLIENT3

first file : /a/b/c/file.sh  first second CLIENT4
second file : /a/b/c/file.sh  first second CLIENT4

I would like to replace line2 for CLIENT2

second file : /a/b/c/file.sh  first second CLIENT2
with
second file : /a/b/c/script.sh  driver file CLIENT2

If I can make CLIENT1,2,3,4 as a variable i will be able to selectively modify the file.
any help is appreciated.

awk '$0~/^second/{sub(".*","second file : /a/b/c/script.sh  driver file "$NF,$0)}1' infile

or

nawk '$0~/^second/{sub(".*","second file : /a/b/c/script.sh  driver file "$NF,$0)}1' infile
1 Like
nawk -v c=CLIENT2 '$NF==c{$(NF-2)="driver";$(NF-1)="file"}1' myFile

i do not have nwak installed.

sed -nr 's#(^second file : ).*( CLIENT2)#\1/a/b/c/script.sh driver file\2#;p' yourFile

replace the parts that you like to varialble.

1 Like

Surprised? Use 'awk' instead.

1 Like

thankyou guys for your valueble input.