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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-02-2013, 01:28 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2013
Posts: 5
Rep: 
|
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
|
|
|
|
01-02-2013, 03:09 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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.
A good resource to learn sed basics: http://www.grymoire.com/Unix/Sed.html.
|
|
|
|
01-02-2013, 03:11 AM
|
#3
|
|
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,592
|
"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.
|
01-02-2013, 03:45 AM
|
#4
|
|
Senior Member
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,191
|
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
|
|
|
|
01-02-2013, 05:25 AM
|
#5
|
|
LQ Newbie
Registered: Jan 2013
Posts: 5
Original Poster
Rep: 
|
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
|
|
|
|
01-02-2013, 05:55 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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.
|
01-02-2013, 07:00 AM
|
#7
|
|
Senior Member
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,191
|
Quote:
Originally Posted by David the H.
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. 
|
|
|
|
01-02-2013, 07:18 AM
|
#8
|
|
Senior Member
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,191
|
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.
|
01-02-2013, 07:29 AM
|
#9
|
|
Senior Member
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,191
|
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
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.
|
01-03-2013, 01:18 AM
|
#10
|
|
LQ Newbie
Registered: Jan 2013
Posts: 5
Original Poster
Rep: 
|
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.
|
|
|
|
01-03-2013, 02:00 AM
|
#11
|
|
Senior Member
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,191
|
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.
|
02-01-2013, 04:37 AM
|
#12
|
|
LQ Newbie
Registered: Jan 2013
Posts: 5
Original Poster
Rep: 
|
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))
|
|
|
|
02-01-2013, 08:37 AM
|
#14
|
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 557
Rep: 
|
echo "$(sed '/horse\|dog/d' < yourfile)" > yourfile
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:00 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|