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

Need Scripting Help

Status
Not open for further replies.

jlmilton

IS-IT--Management
Sep 25, 2008
7
US
I have 2 files with many rows:

First is called Edit_new and has records like:
CN=XXX,CN=M123456,OU=XXX,OU=XXX,OU=XXX,O=XXX,en-du
CN=XXX,CN=M654321,OU=XXX,OU=XXX,OU=XXX,O=XXX,en-vu
Second is:user_list and has records like:
M123456 John Brown
M804420 Alice Cooper
M654321 Joe Bell

What I need is to compare the 2 files and if a record exists in the second file and not in the first file, I want to remove the record from the second file.

Thanks for the help!

Thanks,

 
What have you tried so far and where in your code are you stuck ?
Tip: I'd use an associative array in awk.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Without actually wrining the code for you, here is the approach I would use:
- read the second file, line by line in a loop
- cut out (via awk) the bit to compare in the first file
- ascertain (via grep) if it is indeed in the first file
- if it is, write the whole line to a new file
- go round the loop again, to the end of the second file
- the new file becomes the latest version of the second file

I hope that helps.

Mike
 
Mike, FYI, all can be done with a single awk program ...
 
My use of awk is rather limited.

I have tried awk -F "CN=M" ' {print $2}' editable_langs_new >/tmp/test

but so far, nothing is working. I'll keep tryping. Thanks for the help.
 
What about this ?
awk 'BEGIN{FS="[=, ]"}NR==FNR{++a[$4];next}$1 in a' editable_langs_new user_list

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
feherke, which flavor of grep admit a - as a valid argument for the -f option ?

Yes, I know: GNU grep I presume.
But, Gnu is Not Unix

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I get syntax errors with the awk statement and the lovely bailing out at line ...

On the sed command, my grep does not have a -f option. This is Sun 2.9.
 
Try this:
nawk 'BEGIN{FS="[=, ]"}NR==FNR{++a[$4];next}$1 in a' editable_langs_new user_list

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The nawk command runs but produces a 0 byte file when piped with >>final at the end. If I run it without piping it to a file it just returns to a command prompt.
 
Here the result I get with your posted data:
M123456 John Brown
M654321 Joe Bell

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The version of nawk I am using does not produce anything. I downloaded the gnu version of grep and the command is hanging:

sed -n 's/.*,CN=\([^,]*\).*/^\1 /p' editable_langs_new | /usr/local/bin/grep -f - user_truck_list >user_truck_list_new

Any help would be appreciated.

 
Hi,

Here is a solution using cut, sed, and grep

Code:
mv user_list user_list.bak
cut -f 2 -d, Edit_new | sed 's/CN=/^/g' > memonly
grep -f memonly user_list.bak > user_list
rm memonly
rm user_list.bak

Tested on AIX 5.3 using ksh
 
Hi PHV,

After I typed my suggestion, I thought that it probably could be done in awk/nawk/gawk, etc. But then I have found that awk/nawk/gawk, etc are not as 'portable' as cut/sed/grep, etc and as jlmilton didn't specify hardware or OS I thought a more long-winded (but much more easily maintainable) solution would be an appropriate answer to his/her first post.

Regards.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top