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

Script to grep two fields in seperate lines and compare the two fields

Status
Not open for further replies.

fumang

Technical User
May 14, 2002
7
US
I am having trouble writing a script which will compare two fields in two different lines of all files in a certain directory.

So far I have been able to locate the two fields but do not know how to compare their the two values

this is what I have so far.

for file in "ls *.edi" ; do
grep "ISA" * | cut -f7 -d "*" | cut -f7 -d "|"
grep "GS" * | cut -f3 -d "*" | cut -f3 -d "|"

Now i need to compare these two fields and if they are different echo the filename to the user.

Any help would greatly be appreciated.

Thanks

 
How about this:

for file in "ls *.edi" ; do
A=$(grep "ISA" * | cut -f7 -d "*" | cut -f7 -d "|")
B=$(grep "GS" * | cut -f3 -d "*" | cut -f3 -d "|")
if [ "$A" = "$B" ] ; then
...

The $( ) is a k-shell construct, same as using back quotes.
 
I have 1 question and 1 comment.

What does the * do in grep "ISA" *? Why aren't you using the $file variable like this: grep "ISA" $file?

And you really don't need the ls in for file in "ls *.edi" ; do. The ls is implied inside a for loop so by just using for file in *.edi; do will do what you are wanting. I think Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Thanks for everything
the script is working fine now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top