Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to replace fields in files using AWK? - URGENT

Status
Not open for further replies.

Shantha

Programmer
Jun 12, 2002
48
IN
I need to replace field in one file with field in another file.

File 1

111-000001
111-111111


222-222323
232-343333

File 2

111-000001,AER
111-111111,EXE
111-111111,AER
111-111111,EXE
222-222323,AER
232-343333,AER

If you note file 1 - field 1 has same values as file2 field1 but in the place of repetitive values of file2 file1
contains null. Now I want to replace file2 field1 with file1 field1(mainly i need the null values). The fields in file2 is seperated with null values.
Talent is what you possess;
genius is what possesses you

 
Hi Shantha,

I am making some assumptions about your files:

1) Your files have a 1 to 1 correspondance, such that record n in file 1 will always correspond to record n in file 2, and

2) There if a line is blank in file 1, there is only a newline.

Anyway, if I understand your problem correctly, I think the following may solve your problem. I have not tested it as I do not currently have access to awk, but the idea should be sound:

% cat my.awk
#!/usr/bin/awk -f
BEGIN {
FS=",";
OFS=",";
}

{
getline line < first;
$1=line;
print($0) > outfile;
}


The syntax to run at the command line is:
% my.awk outfile=<output file> first=<file 1> <file2>

Example:
% my.awk outfile=out.txt first=file1.txt file2.txt

By the way, if there really is a 1:1 correspondance between these files, and if the records are sorted, it would probably be just as simple to write the script using only file2 as input.

Hope this helps,
Grant.

PS: If I have misunderstood your requirements, please clarify.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top