April 09, 2009

UNIX case study - 14

Replace text in another file using awk


I have a script. The required field is getting replaced with $name/$field2..

for name in $(nawk -F'|' -v OFS='|' '$2="$name"' temp2 > temp3
done

Files
temp4
gg
re
tt
vv
qq

temp2
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz


outputf file(temp3)
11|$name|33|44|zz
11|$name|33|44|zz
11|$name|33|44|zz
11|$name|33|44|zz
11|$name|33|44|zz

required output
11|gg|33|44|zz
11|re|33|44|zz
11|tt|33|44|zz
11|vv|33|44|zz
11|qq|33|44|zz


My requirements
===================

file1
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz


file2
aa|bb|cc1|dd|55
aa|bb|cc2|dd|55
aa|bb|cc3|dd|55
aa|bb|cc4|dd|55
aa|bb|cc5|dd|55


required output
11|22|cc1|44|zz
11|22|cc2|44|zz
11|22|cc3|44|zz
11|22|cc4|44|zz
11|22|cc5|44|zz


I need to replace the 3rd field of file1 with 3rd field of file2 for all records(1st record should be replaced with 1st record only of the other file)
Both files have same number of lines as well as fields.


http://www.unix.com/shell-programming-scripting/106742-help-replace-field-one-file-field-another-file.html

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.