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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare file1 and file2 replacing field in f2 ignoring case

Status
Not open for further replies.

mhbjr

IS-IT--Management
Dec 19, 2011
5
US
Thanks for any help.
I have 2 files and I am reading in the second file and reformating it with output going to a third file. My problem is I need to read the first file and compare with second file and if data in a field matches I need to replace it with the data from the first file. The compare has to ignore case.
File 1:
FieldType: Text
FieldName: loan_dt
FieldNameAlt: enter date of loan
---
FieldType: Text
FieldName: Contract_nmbr
FieldNameAlt: enter contact number

and so on.

File 2:

^FIELD LOAN_DT
12/31/2011
^FIELD CONTACT_NMBR
A351232

and so on.

I have a script that does all of the reformating and output to file 3 but what I need is if:

File 2: If ($1 == "^FIELD") I need to use $2 data to search File1 and if found replace what is in $2 with data from File1 so I can send it to output.

I know I probably need to array File1 but have not been able to get anything to work.

I am trying to get LOAN_DT to be loan_dt and CONTACT_NMBR to be Contact_nmbr.

Thanks,
 
Hi

Tried to use the [tt]tolower()[/tt] or [tt]toupper()[/tt] function ?


Feherke.
 
Thought of it but needs to be idential to File1 which can be a mix of upper and lower.
 
The key is to convert both sides of the comparison before comparing them. You don't have to actually store the converted values. E.g. if (tolower(a) == tolower(b)).

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks all, I was able to solve the problem. What I didn't know until I debuged it with the sh -x command is that when I move the data from the file to a variable i get the data\r So I solved it with the following command.

for i in `<file1 awk '{$1 == "FieldName:"} print $2}' |
awk '{sub(/\r$/,"");print $1}'`
do
sed -i "s/${i}/${i}/I" file2
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top