Compare 2 list and delete certain names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 list and delete certain names
# 1  
Old 08-21-2007
Compare 2 list and delete certain names

Hi,

I currently have a script that takes a list of names and compares it with another list and appends non-duplicate names. I want to modify my script such that it will look at a list of names and for every name preceded by the tag "<delete>" (without the quotes) it checks the other list for that name and will remove it and with whatever else, it will perform the regular functionality of the script like before. So here's an example:

List1:
Jim
Bob
Ed
Hank

List2:
Greg
<delete>Hank
Dave

Result of List1 after the script run:
Jim
Bob
Ed
Greg
Dave

Can anyone help here? Thanks!
# 2  
Old 08-22-2007
HI
I Hope this script works




#!/usr/bin/ksh



while read line
do
while read value
do
grep "<delete>" |pattern=`cut -d">" -f2`
if [ $pattern == $line ]
then
sed /$pattern/d file1
sed /$pattern/d file2 |sed '/^ *$/d'
fi
done <file2
done <file1
# 3  
Old 08-22-2007
Try this

Code:
sed -n '/^\<delete\>/d' filename

# 4  
Old 08-22-2007
Quote:
Originally Posted by aajan
HI
I Hope this script works




#!/usr/bin/ksh



while read line
do
while read value
do
grep "<delete>" |pattern=`cut -d">" -f2`
if [ $pattern == $line ]
then
sed /$pattern/d file1
sed /$pattern/d file2 |sed '/^ *$/d'
fi
done <file2
done <file1
Thanks! I'm assuming file1 and file2 are equivalent to my List1 and List2 right? So I tried running this and it gave me:

./other.sh[10]: [: argument expected
./other.sh[10]: [: argument expected
./other.sh[10]: [: argument expected
./other.sh[10]: [: argument expected
./other.sh[10]: [: argument expected
./other.sh[10]: [: argument expected
Jim
Bob
Ed
Greg
Dave
./other.sh[10]: [: argument expected
./other.sh[10]: [: argument expected


It seemed to have compiled the list right but I'm not sure of the errors. I ran the script pretty much like "./script.sh" Not sure if I was supposed to pass anything into it. Could the new list be stored in some file?

Last edited by eltinator; 08-22-2007 at 01:49 PM.. Reason: update
# 5  
Old 08-22-2007
ok nm, I wrote up a similar script that did it for me. Thanks for the help anyways everyone!
# 6  
Old 08-22-2007
Hi
the script is fine.

just change the if loop used to
if [ "$pattern" = "$line" ]

Also the script is hard coded.
If you dont have any delete tag in file2, it wud display nothing.
on other hand if you have 2 delete tags it will display the results twice...
if its ok for u ... fine...
or else we ll go for a better solution...

Happy scripting!!
# 7  
Old 08-22-2007
Try this:

Code:
for var in $(cat list2); do

echo $var | grep "^<delete>" > /dev/null
if [[ $? -eq 0 ]]; then
Name=$(echo "$var" | cut -d">" -f2 )
echo $Name
grep -i "$Name" list1 >/dev/null
if [[ $? -eq 1 ]]; then
echo $var
fi

fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare files with different names in different directories

Hi, I have a requirement to compare files in different directories with different names. The files have a datestamp in their name (It might not be a sequential datetimestamp). This is for Redhat Linux. I have more than 5 directories and more than 10 file in each directory to be compared. ... (4 Replies)
Discussion started by: GosarJunk
4 Replies

2. Shell Programming and Scripting

Compare two files containing package names and version number

I have 2 files each containing a list of same fedora packages but with different version number. I want to compare the 2 files and remove the lines containing a newer or older version number (1 Reply)
Discussion started by: asya18
1 Replies

3. Shell Programming and Scripting

Compare two files and get only missing names

I need to compare two files (oldfile1 & newfile). Need to ignore the values which are present in both files. At the same time, i need to get only records in new file. Tried using Join -v1 -v2 oldfile1 newfile (suspect it has not worked as expected). could anyone of you please help me here. (5 Replies)
Discussion started by: Selva_2507
5 Replies

4. Shell Programming and Scripting

Compare file names on directory

Dears, Would you please help on following bash script: I want to get the most recent file named alfaYYYYMMDD.gz in one directory: for example: in directory /tmp/ ls -ltr alfa20130715.gz holding.gz alfa20130705.gz sart.txt merge.txt.gz alfa20130802.gz my result shoud be... (1 Reply)
Discussion started by: maxsub
1 Replies

5. Shell Programming and Scripting

Check for particular files and compare the file names

Hi, Below are the 2 files in directory /tmp: masterCSF242323.img indexCSF242323.img 1) I want to compare if both the number (242323) are same in both the files. If they are same print - Files matching, else print files do not match. 2) Also if only index file is present in that... (7 Replies)
Discussion started by: apatil65
7 Replies

6. UNIX for Dummies Questions & Answers

how to compare names of files?

hi, can somebody tell me how to compare names of files? the situation is I have 2 files file1 and file2 and I want to figure out which file has the biggest ending, in this case file2 is. thank you (3 Replies)
Discussion started by: s3270226
3 Replies

7. Shell Programming and Scripting

Compare list [ names and size files ]

Hello, I've downloaded a huge amont of files I've got a list of files from a remote server. -rw-r--r-- 1 str661 strem 453465260 Dec 16 15:54 SATRYS2V1_20021218_temp_bias.nc -rw-r--r-- 1 str661 strem 17669468 Dec 16 18:01 SATRYS2V1_20021225_hdyn_bias.nc -rw-r--r-- 1... (9 Replies)
Discussion started by: Aswex
9 Replies

8. Shell Programming and Scripting

Compare File Names in Different Directories...

I do not know much about shell scripting and need to create a script and I am at a loss. If someone can help me, that would be great!! I have two directories: /dir1 /dir2 I need to get the sequence number which is part of the filename in /dir1 and delete all files in /dir2 that are... (4 Replies)
Discussion started by: stky13
4 Replies

9. UNIX for Dummies Questions & Answers

how to delete file names with $ in them

I'm reading the file name from a spreadsheet then creating a data file with several remove statements. i.e cd /sun/5.0/AWBE/v1.0/svr/VaultLocs/CPVaultLoc/ rm "ZXCV KTest Stat host $$ cont.xls" cd /sun/5.0/AQE/v1.0/svr/VaultLocs/CPVaultLoc/ rm "ABC PMT OzLtr.pdf" cd... (2 Replies)
Discussion started by: orahi001
2 Replies

10. Shell Programming and Scripting

Compare file names

Hi everyone, How to compare between two filenames, in case the current filename is the same as the last one an alarm to be sent, in case the current filename is different from the last filename, no alarm to be sent. Is there a way to do this? Thanks in advance. (4 Replies)
Discussion started by: charbel
4 Replies
Login or Register to Ask a Question