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!

sed in a for loop

Status
Not open for further replies.

mhbjr

IS-IT--Management
Dec 19, 2011
5
US
I need help! I am trying to use sed in a for loop and does not seem to be working correctly.

If I have a file called file2 that contains:
Field: LOAN_DATE

and I do:
i="loan_date"
sed -i "s/${i}/${i}/I" file2

This works replacing LOAN_DATE with loan_date

The problem is when I put this in a for loop it doen't work. I need it to loop on the file called file1 that contains multiple entries for compare and replace. File Examples:

file1 contains:
Field: loan_date
Field: Contact_no
Field: loan_NO
Field: acctount_no

file2 contains;
Field: LOAN_DATE
Field: CONTACT_NO
Field: LOAN_NO
Field: ACCOUNT_NO

My scritp is:

for i in `cat file1 |awk '{if $1 == "Field:") print $2}'`
do
sed -i "s/${i}/${i}/I" file2
done

Setting sed up to replace in file ignoring case. I get no change to the file.

Thanks for any help.
 


Here is one alternative:
Code:
cut -d':' -f2 <file1 | awk '{print "s/"$1"/"$1"/I";}' >chg.sed
sed -f chg.sed <file2
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Or without temp file:
Code:
sed -f <(cut -d':' -f2 <file1 | awk '{print "s/"$1"A/"$1"/I;";}') <file2
[noevil]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks I finally figured it out. When I get the data from the file into $i it has the \r on the end of the data ex. "loan_dt\r" cannot see this with any editor finally found it by doing sh -x to debug the shell. Didn't know that command. I fixed it by running it through another awk statement to remove the \r.

This is my ending script.

for i in `< nxx028.dfd awk '{if ($1 == "FieldName:") print $2}' | awk '{sub(/\r$/,"");print $1}'`
do
sed -i "s/${i}/${i}/I" nxx028.fdf
done


Thanks Again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top