Hello People,,
I have two file file1.txt file2.txt,both file have content like following
file1.txt
v1 2 3 dc 5v
v2 4 5 dc 6v
v3 6 7 dc 2v
file2.txt
2
3
4
the number in last coloumn of file1.txt should be replaced by number in file2.txt as follows
v1 2 3 dc 2v<-----2 is coming from file2.txt
v2 4 5 dc 3v<-----3 is coming from file2.txt
v3 6 7 dc 4v<-----4 is coming from file2.txt
I do not know how to do it.I want to write a Shell script for it.Please help.,
If the letter in the last field is always a "v":
awk '{getline s < "file2.txt"; $NF=s "v"}1' file1
Otherwise:
awk '{getline s < "file2.txt"; sub(int($NF),"",$NF);$NF=s $NF}1' file1.txt
Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.
Hi Franklin
awk '{getline s < "file2.txt"; $NF=s "v"}1' file1.txt
it worked for me,Thanks
can you please let me know $NF=s "v"}1 what this part is doing i mean what this 1 is doing in last because without 1 in output nothing is coming
my @arr=qw/2 3 4/;
while(<DATA>){
s/([0-9]+)(?=[a-z]+$)/$arr[$.-1]/e;
print;
}
__DATA__
v1 2 3 dc 5v
v2 4 5 dc 6v
v3 6 7 dc 2v
An awk statement has the form:
condition {action}
Conditions in awk control the execution of actions and actions are executed when the condition is true.
If the condition is true (1 is true) and there are no actions between braces, awk prints the current record by default.
Regards
1 Like
awk 'NR==FNR{a[NR]=$1;next}{$NF=a[FNR]"v"}1' file2.txt file1.txt