1833024 Members
2505 Online
110049 Solutions
New Discussion

Script help

 
SOLVED
Go to solution
Christian Briddon_1
Occasional Advisor

Script help

Hi there,

I am trying to write a script that takes 2 lists of userID's and removes the contents of one from the other. For example, I am using the cut command to remove the first field from the /etc/passwd file. This leaves me with my first list of userID's. I also have a manually created list of users. This second list is much shorter.

I want a script that removes the users in the second (shorter) list from the longer passwd file generated list and outputs the remaining users to a file.

I'm sure some of you guys can help me out on this and I will be very greatful if you could do so.

Cheers,

Christian Briddon
11 REPLIES 11
Stefan Schulz
Honored Contributor

Re: Script help

Hi Chsirtian,

i don't have a system at hand right now. But i think something like this will work in the normal shell:

while read user
do
grep -v $user longfile > tmpfile
mv tmpfile longfile
done < shortfile

Where shortfile is your manually created list. Lonfile is your extract from /etc/passwd. And tmpfile is just a temp. file like /tmp/working.lst

But please do a test with some copied lists first. I don't want to be responsible for any loss of data ;-)

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Rich Wright
Trusted Contributor

Re: Script help

Try this:
diff xx1 xx2 | grep '^<' | sed 's/< //' > newlist
harry d brown jr
Honored Contributor
Solution

Re: Script help

# cat /tmp/short
abc10
abc99
def5
# cat /tmp/long
abc01
abc02
abc03
abc10
abc55
abc77
abc99
bef01
def5
def6
# cat /tmp/long | grep -v -f /tmp/short
abc01
abc02
abc03
abc55
abc77
bef01
def6
#


live free or die
harry
Live Free or Die
MANOJ SRIVASTAVA
Honored Contributor

Re: Script help

Hi

let us suppose file A is the smaller file , and file B is the bigger file , to create file which should have all the users in file B but not in A then you can do like this


cat file A |
while read user
do
cat B | grep -v $user >> new file
done


this will do the job.

Manoj Srivastava
Stefan Schulz
Honored Contributor

Re: Script help

No system, no man pages :-(

i think Harry's method is the best and fastest. So 10 points to him.

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Stefan Schulz
Honored Contributor

Re: Script help

BTW:

you don't have to use cat here. grep will read from the file directly. So using Harry's solution, this should be the fastest:

grep -v -f /tmp/short /tmp/long

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Rich Wright
Trusted Contributor

Re: Script help

I recend my first suggestion.
I like
grep -v -f short_file long_file > new_file
With the exception of having entries in the short_file look like "^userid$" instead of just "userid".
Then you will not remove "abc10", "abc1", or "test_abc" with a short_file entry of "abc".
harry d brown jr
Honored Contributor

Re: Script help

Adding the "-x" option will satisfy the request:

# cat /tmp/short
abc10
abc99
def5
# cat /tmp/long
abc01
abc02
abc03
abc10
abc55
abc77
abc99
bef01
def5
def6
def56
def65
# cat /tmp/long | grep -v -x -f /tmp/short
abc01
abc02
abc03
abc55
abc77
bef01
def6
def56
def65
#

or as put by Stefan:

grep -v -x -f /tmp/short /tmp/long

live free or die
harry
Live Free or Die
Jordan Bean
Honored Contributor

Re: Script help


comm -23 /path/to/longlist /path/to/shortlist

Leif Halvarsson_2
Honored Contributor

Re: Script help

Hi

As I can see your problem already is solved but if you are interested in shell programming
there is a command just for your need called "join". Have a look at it (man join).

In the example above it should look:

join -v 2 /tmp/short /tmp/long
Christian Briddon_1
Occasional Advisor

Re: Script help

Just like to say a big thanks for all of your posts. The script is now working fine and I have allocated points accordingly.

Christian Briddon