Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn unix and linux commands

delete lines from file2 beginning w/file1

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-16-2003   -   Original Discussion by michieka
michieka's Unix or Linux Image
michieka michieka is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 4 August 2006, 2:39 PM EDT
Location: Raleigh, nC
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
delete lines from file2 beginning w/file1

I've been searching around here and other places, but can't put this together...

I've got a unique list of words in file 1 (one word on each line).
I need to delete each line in file2 that begins with the word in file1.

I started this way, but want to know how to use file1 words instead of supplying a keyword:

sed -e '????/d' < file2 > file3

sed -e '`cat file1`/d' 012703.csv 061603.csv

I want to put something at the ???? that reads each line of file1. Any help would be greatly appreciated!

LSM
Sponsored Links
    #2  
Old Unix and Linux 06-17-2003   -   Original Discussion by michieka
oombera's Unix or Linux Image
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 6 Times in 5 Posts
while read WORD
do
sed '/'$WORD'/d' file2
done < file1
Sponsored Links
    #3  
Old Unix and Linux 06-17-2003   -   Original Discussion by michieka
michieka's Unix or Linux Image
michieka michieka is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 4 August 2006, 2:39 PM EDT
Location: Raleigh, nC
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
what about file3?

Thanks! I want to put the new file2 (with the lines already deleted) into a new file3. Also, I need to make sure $WORD is at the beginning of the line. I tried the below but got an empty file3.

while read WORD
do
sed '/$WORD/d' file2 > file3
done < file1

Any suggestions?
    #4  
Old Unix and Linux 06-17-2003   -   Original Discussion by michieka
Ralf's Unix or Linux Image
Ralf Ralf is offline
Registered User
 
Join Date: Oct 2002
Last Activity: 24 August 2006, 8:48 AM EDT
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Use a for loop?



Code:
for Word in `ls <unique list of words filename>`
do

 sed -e '$Word/d' < file2 > file3

done

Sponsored Links
    #5  
Old Unix and Linux 06-17-2003   -   Original Discussion by michieka
oombera's Unix or Linux Image
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 6 Times in 5 Posts
See if this works:

Code:
cp file2 file3
while read WORD
do
  sed '/'$WORD'/d' < file3 > TMP_00
  mv TMP_00 file3
done < file1

or this, which may be faster:

Code:
while read WORD
do
  cmd="$cmd -e /$WORD/d"
done < file1

`sed $cmd < file2 > file3`

Sponsored Links
    #6  
Old Unix and Linux 06-19-2003   -   Original Discussion by michieka
michieka's Unix or Linux Image
michieka michieka is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 4 August 2006, 2:39 PM EDT
Location: Raleigh, nC
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
That works! Now I want to make sure that $WORD is at the beginning of the line. Shouldn't this work?

while read WORD
do
cmd="$cmd -e /^$WORD/d"
done < file1

`sed $cmd < file2 > file3`

THIS IS file1 CONTENTS:

15
16
17

THIS IS file2 CONTENTS:

15
16
What is the meaning of 17
18

I WANT file3 TO BE WRITTEN AS:

What is the meaning of 17
18

That is... lines 1 and 2 were deleted because they began with 15 and 16... Right now I'm getting file3 that looks like this:

18

Thanks for any help!
Sponsored Links
    #7  
Old Unix and Linux 06-19-2003   -   Original Discussion by michieka
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 63
Thanked 471 Times in 271 Posts
I can't duplicate your failure. I get the two lines that you want. What shell are you using? Can you try it with ksh or bash?

And you don't need those backquotes around the sed command. Try getting rid of those.
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove lines in file1 with values from file2 palex Shell Programming and Scripting 2 01-22-2012 01:47 AM
[Solved] delete line from file1 by reading from file2 senayasma Shell Programming and Scripting 7 10-28-2011 12:19 PM
Display lines from file1 that are not in file2 chebarbudo Shell Programming and Scripting 5 09-08-2010 10:05 PM
awk/sed search lines in file1 matching columns in file2 floripoint Shell Programming and Scripting 1 12-17-2008 11:36 PM
extracting lines from a file1 which maches a pattern in file2 smriti_shridhar Shell Programming and Scripting 11 07-31-2008 01:56 AM



All times are GMT -4. The time now is 09:13 PM.