LinuxQuestions.org
Help answer threads with 0 replies.
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-02-2013, 01:28 AM   #1
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Rep: Reputation: Disabled
delete a matched line from the file+shell script


Hi all,
I am matching some lines in a file. If my choice of line is matched, that line should be deleted from the file without using intermediate files.
Please help me.
Please note that the lines in file contains only word. So deleting the entry itself makes sure line is deleted. Any help is appreciated
for i in dog horse
do
<<Delete lines with $i on $file >>
done
 
Old 01-02-2013, 03:09 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
A simple sed command should do the trick. Take a look at the -i option to edit the file in place without creating temporary files.
Code:
man sed
info sed
A good resource to learn sed basics: http://www.grymoire.com/Unix/Sed.html.
 
Old 01-02-2013, 03:11 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,751

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
"without using intermediate files" -this is basically impossible. Even when you use 'sed -i' it will create a temposrary file. The only way to avoid this would be to read each file fully into an array, manipulate the arrays and then overwrite the original files once done.
 
1 members found this post helpful.
Old 01-02-2013, 03:45 AM   #4
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,812

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
With ed perhaps, where the file is called 'yourfile' and you are deleting lines with 'horse' and 'dog' (as per your example):

Code:
ed -s yourfile <<< $',g/\(horse\|dog\)/d\nw'
EDIT Note: The above assumes you are using bash. David the H. provides a shell agnostic version of this below

EDIT This page a useful resource to get you started on editing files in place with ed: http://wiki.bash-hackers.org/howto/edit-ed

Last edited by ruario; 01-02-2013 at 07:03 AM. Reason: Added a link, added a note that I am using bashisms
 
Old 01-02-2013, 05:25 AM   #5
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi,
I have tried sed. It worked in linux but did not work on solaris. How can I overcome this? I want this to work on both the platforms.
sed: illegal option -- i
 
Old 01-02-2013, 05:55 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
It usually helps to mention things like which platform you're using, you know. Since this is LinuxQuestions, we generally assume you're using Linux unless stated otherwise.

-i is a non-posix gnu extension to sed, and not available in most other implementations. There's no way to get those versions to save directly to file.

(Although you could capture its stdout to a variable, for example, then echo the the modified version back over the original file.)


It's easier just to use ed, as shown above. Since ed is also posix, the command syntax posted will probably work anywhere, although you may need to adjust the shell syntax if you aren't using bash/ksh. I usually prefer to pipe the commands into it with printf myself.

Code:
printf '%s\n' 'g/\(horse\|dog\)/d' 'w' | ed -s yourfile

Last edited by David the H.; 01-02-2013 at 06:04 AM. Reason: additions
 
2 members found this post helpful.
Old 01-02-2013, 07:00 AM   #7
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,812

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
Quote:
Originally Posted by David the H. View Post
although you may need to adjust the shell syntax if you aren't using bash/ksh. I usually prefer to pipe the commands into it with printf myself.
Fair point, I shouldn't have assumed bash in my example. Thanks for providing the OP with a less shell specific version.
 
Old 01-02-2013, 07:18 AM   #8
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,812

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
To the OP, it might help to think of it like this instead:

Code:
printf '%s\n' 'g/horse/d' 'g/dog/d' 'w' | ed -s yourfile
Yes, it is longer but perhaps easier to understand that you simply need to tweak (or add further) 'g/regex/d' for each deletion.

Last edited by ruario; 01-02-2013 at 07:20 AM. Reason: formatting
 
1 members found this post helpful.
Old 01-02-2013, 07:29 AM   #9
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,812

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
Also be careful with your regexes. If one of them matches a substring from another line you could easily delete too many lines, e.g. the above examples would also kill a line that stated 'dogeared'.

Quote:
Originally Posted by novicunix View Post
Please note that the lines in file contains only word.
In that case, this would probably be safer (assuming no spaces before or after the text):

Code:
printf '%s\n' 'g/^horse$/d' 'g/^dog$/d' 'w' | ed -s yourfile
 
1 members found this post helpful.
Old 01-03-2013, 01:18 AM   #10
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi,
Thanks for providing info on 'ed'.
But instead of "printf '%s\n' 'g/horse/d' 'g/dog/d' 'w' | ed -s yourfile", I want to run this in a loop with the variable to be deleted.
I wanted to try "printf '%s\n' 'g/$i/d' 'w' | ed -s yourfile". This did not work.
 
Old 01-03-2013, 02:00 AM   #11
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,812

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
Use double quotes rather than single quotes in that case:

Code:
printf "%s\n" "g/$i/d" "w" | ed -s yourfile
 
1 members found this post helpful.
Old 02-01-2013, 04:37 AM   #12
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
check Negative if condition

Hi
I need to check a negative egrep condition. How is it possible?
Like if (!(cat file | egrep -e 'x|y|z))
 
Old 02-01-2013, 08:10 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Don't post your questions in multiple places. One thread per topic, please.

http://www.linuxquestions.org/questi...on-4175448130/
 
Old 02-01-2013, 08:37 AM   #14
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 582

Rep: Reputation: 129Reputation: 129
echo "$(sed '/horse\|dog/d' < yourfile)" > yourfile
 
  


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
delete a line from file using shell script Abid Malik Programming 2 10-16-2010 02:01 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 07:13 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