Operating System - HP-UX
1748089 Members
4855 Online
108758 Solutions
New Discussion юеВ

some shell script or commands

 
SOLVED
Go to solution
Shivkumar
Super Advisor

some shell script or commands

Hello,

We have a text file "test1" containing both "required" and "un-required" server listing.
We created another text file "test2" from above containing only "un-required" server listing.

This test1 is a large file containing hundreds of server names listing.

Now i want to remove un-required servers from file test1.

Can anyone suggest some quick way to do it ?

Thanks,
Shiv
5 REPLIES 5
Rita C Workman
Honored Contributor

Re: some shell script or commands

If your test1 containing both required and un-reguired shows something like this:

required server1
un-required serverabc
required serverxxx
un-required node123

Then you could:

cat test1 | grep -v "un-required" > test.new

Just a very very quick thought,
Rita
Patrick Wallek
Honored Contributor

Re: some shell script or commands

To get only REQUIRED from the file test1 into newfile do:

grep -v -f test2 test1 > newfile

What this does:

-v - output non-matching strings
-f test2 - use the patterns in this file

See the grep man page for more information.
Suraj K Sankari
Honored Contributor

Re: some shell script or commands

HI,
See the example below

[suraj@rspc521 tmp]# cat srvlist
server1
server2
server3
server4
server5
server6
server7
server8
server9

[suraj@rspc521 tmp]# cat unreqlist
server2
server3
server5
server8
server9

[suraj@rspc521 tmp]# diff srvlist unreclist
1d0
< server1
4d2
< server4
6,7d3
< server6
< server7


The output is your required servers list.

Suraj
Andrew Rutter
Honored Contributor

Re: some shell script or commands

hi,

it depends upon how the text is in the file to a certain extent?

if for example you have them organised into columns of data, and all the un-required ones are in one or two coloumns then this could be achieved by something like this

example test file

required unrequired sorted

server1 server2 server5
server3 server4 server7

then we could extract the other two columns of data into another file

#awk '{print $1,$3,}' test > test3

Andy



James R. Ferguson
Acclaimed Contributor
Solution

Re: some shell script or commands

Hi Shiv:

Your file formats would help provide an appropriate solution.

If the files are sorted, and, for example, look like:

# cat file1
server1
server2
server3
server4
server5
server6

# cat file2
server2
server4
server6

...then:

# comm -23 file1 file2
server1
server3
server5

Regards!

...JRF...