Help on Sed/awk/getting line number from file

I Have file1 with below lines :

#HostNameSelection=0 :NotUsed
#HostNameSelection=1 :Automatic
#HostNameSelection=3 :NotForced

I have file2 which has similar lines but with different values

I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'.
for other lines i am doing pattern matching of LHS of file1 with LHS of file2 and copying new Values to RHS of file2.

Please Help.

man comm
man diff
egrep -e '^#' file1 >f1
egrep -e '^#' file2 >f2
diff f1 f2

?

this will give me the difference in the values of lines beginning with # , how to copy the value at same line?

Please give more clue about how do your 2 files look like and how should your output look like

reffile:
IP = 10.10.10.102;
#HostNameSelection = 0 ;
#DomainName = abc.com;#url1
Targetfile:
IP = 127.0.0;
#HostNameSelection = 1 ;
#DomainName = xyz.com;#url2

Targetfile should be modified as per the content of Reffile after running Shellscript of upgrade.

You don't want just copy since you only want #line changes?

file1 is reffile
file2 is Targetfile

egrep -ve '^#' file2 >file2.tmp
egrep -e '^#' file1 >>file2.tmp
cat file2.tmp >file2
rm file2.tmp

I dont want to disturb the file2,changes should be done at the same place as they appear in file2.

The modified # lines should not be appended at the end .

Targetfile:
IP = 127.0.0;
#HostNameSelection = 1 ;
host = abbb;
#DomainName = xyz.com;#url2
destination = annana;
userid = 123;

The Line & ordering should remain intact.

Could you do the following :

grep -n ^# reffile >f1
grep -n ^# Targetfile >f2

and upload f1 & f2

---------- Post updated at 01:50 PM ---------- Previous update was at 01:31 PM ----------

Maybe you can try something like :

awk 'NR==FNR&&/^#/{A[NR]=$0}NR!=FNR{if (/^#/) if ($0!=A[FNR]) sub(".*",A[FNR]);print $0}' reffile Targetfile

But this assumes that the lines to be compared must be at the SAME line number respectively in their own files

1 Like