1843095 Members
2245 Online
110213 Solutions
New Discussion

Scripting help

 
SOLVED
Go to solution
Terrence
Regular Advisor

Scripting help

My scripting skills are woeful I'm afraid. I generate a file that contains only a list of user id's. Whenever I generate this file, I need to remove the same 10 user id's that always appear and leave the remaining id's. I've been doing this manually in vi but now I need to automate the process.

Simplest and/or most elegant and/or easiest to understand solution gets the full 10!!.
12 REPLIES 12
RAC_1
Honored Contributor
Solution

Re: Scripting help

grep -Ev "user1|user2|upto_10_users" "input_file" > new_file

If they appear after certain line in file, then

head -"line" inout_file OR tail -"line" input_file

Anil
There is no substitute to HARDWORK
Jean-Luc Oudart
Honored Contributor

Re: Scripting help

grep -v user1 | grep -v user2 | grep -v user3 | ...
>

Regards,
Jean-Luc
fiat lux
H.Merijn Brand (procura
Honored Contributor

Re: Scripting help

# your_script_that_generates_the_list | grep -v -w -e 203 -e merijn -e skipme -e baduser -e 488 > zzz

# your_script_that_generates_the_list | perl -pe's/\b(203|merijn|skipme|baduser|488)\b//g' >zzz

Enjoy, Have FUN! H.Merijn [ who doen't know if -w is supported in all grep's on your machine ]
Enjoy, Have FUN! H.Merijn
Simon Hargrave
Honored Contributor

Re: Scripting help

As above, but I'd also put anchors in place, eg

grep -vE "^uid1$|^uid2$|^uid3$" file > newfile

The ^ and $ anchors ensure that for example removing "fred" doesn't also remove "frederick".

If you want to edit the file in-situ, you could do: -

ed file </^uid1$
d
/^uid2$
d
/^uid23$
d
w
q
EOF

This will let you do it without a temporary "newfile".
Jeff_Traigle
Honored Contributor

Re: Scripting help

Several ways depending what IDs you're trying to eliminate...

If they are the reserved users with UID < 100, you can simply use awk as follows:

awk '{if ($3 < 100) print}' /etc/passwd > newfile

If there are specific normal user's you're talking about, you can do similar to above specifying the UIDs of those users combined with the and operator:

awk '{if ($3 != 250 && $3 != 300) print}' /etc/passwd > newfile

or by username:

awk '{if ($1 != "joebob" && $1 != "nancysue") print}' /etc/passwd > newfile
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Scripting help

Oops... first one should have been >, not <
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Scripting help

Argh... actually >= with 100 as the right-hand operand... maybe I should just go home now. :)
--
Jeff Traigle
Rick Garland
Honored Contributor

Re: Scripting help

Can use the "uniq" funtion as well.

cat /etc/passwd | awk -F: '{print $1, $2}' | uniq

The uniq function will get rid of repeats
Terrence
Regular Advisor

Re: Scripting help

Many thanks to all who played!

10 points to RAC for his insanely fast and simple answer. (Doh! grep! I know grep! Why didn't I think of that!)

10 points to Simon for his excellent addition to the simple answer that prevented a problem that I've had in the past where too much is removed. I'll be writing this solution down for other scripts I have as well.

10 points to Rick who unknowingly gave me a solution to a completely different scripting problem I was mulling.

Muthukumar_5
Honored Contributor

Re: Scripting help

We can do with awk script as,

awk '{ for ( i=1; i<=NF; i++) if ( $i != "user1" && $i != "user2" && $i != "user3" ) print $0 }'

IT will give the way to get user lists except your users.

HTH.
Easy to suggest when don't know about the problem!
H.Merijn Brand (procura
Honored Contributor

Re: Scripting help

Terence, not fishing for points here, but that -w option is exactly the same as the proposed anchors, only much cleaner: it means "match words only"

And in the perl statement, I did exactly the same: \b is word bound

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Andrew Merritt_2
Honored Contributor

Re: Scripting help

You also have the option to put the list of user ids in a file, rather than putting them in the command, which may be less prone to mistyping if you edit the script, e.g. if the list ever changes.

So, if the list of user ids (one per line) is /tmp/uids:

grep -Fwv -f /tmp/uids file

should do what you want.

(As you're dealing with fixed strings, -F is probably faster than -E).

Andrew