LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-31-2007, 11:14 AM   #1
bhepdogg
LQ Newbie
 
Registered: Sep 2006
Distribution: Ubuntu 9.04
Posts: 13

Rep: Reputation: 0
Bash remove part of a file based on contents of another file


I have file x and file y. File y contains a lot of text, including the contents of file x (at some point, the command cat x >> y had been issued). Is there a quick and easy way to reverse this? Basically this means removing from file y, the part that matches the exact contents of file x?

For example, if file y contains:
a
b
c
d
e
f
d
c

and file x contains:
c
d

then the desired output would be file y containing:
a
b
e
f
d
c
 
Old 01-31-2007, 12:18 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I'm not aware of how you can do this by scripting only in bash. I have three questions for you.

1. Are you willing to bite the bullet and try Perl? It could be rather simple in Perl.

2. Is either of the two input files likely to be huge huge huge, or are they of reasonably short length?

3. Suppose the content of the file x is found more than once in file y. Do you want to remove all occurrences, or just the first one?
 
Old 01-31-2007, 12:24 PM   #3
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
file "x" contents -

first line
...
...
....
..
last line

sed 's/first line/last line/d' y

this deletes all lines between 1st and last line
check if it works and then run "sed" with "-i" option

sed -i.bak 's/....
(".bak" creates backup file )

Last edited by ygloo; 01-31-2007 at 02:12 PM.
 
Old 01-31-2007, 02:19 PM   #4
bhepdogg
LQ Newbie
 
Registered: Sep 2006
Distribution: Ubuntu 9.04
Posts: 13

Original Poster
Rep: Reputation: 0
Talking

Thanks a bunch. That was exactly what I was looking for. Here is my script that works in case anyone else would like it:

#!/bin/bash

FIRSTLINE=`head -1 x`
LASTLINE=`tail -1 x`

sed -i "/^$FIRSTLINE$/,/^$LASTLINE$/d" y
 
Old 01-31-2007, 03:13 PM   #5
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
with "^" sed will search for pattern at begging of line

sed -i "/$FIRSTLINE$/,/$LASTLINE$/d" y

found info here:
http://www-128.ibm.com/developerwork...-textedit.html

Last edited by ygloo; 02-23-2007 at 04:01 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: date based file maintenance paul.anderson Programming 4 07-19-2006 06:23 AM
/bin/bash how to remove specific file anywhere on harddrive cjagdish69 Linux - Software 1 01-07-2005 05:42 AM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 01:23 PM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM
[bash] remove lines from a file Drimo Programming 3 03-20-2004 11:16 AM


All times are GMT -5. The time now is 10:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration