|
|||||||||
| 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 |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
while read WORD
do sed '/'$WORD'/d' file2 done < file1 |
| Sponsored Links | ||
|
|
|
#3
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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 | ||
|
|
![]() |
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 |
|
|