Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-03-2009, 03:32 AM   #1
LQ Newbie
Registered: Nov 2006
Posts: 18

Rep: Reputation: 0
Question Trouble with sed substitution and file output

I am trying to put together a relatively simple script to comment out lines in a file which match a user/email combination. In the file fields are delimited with '/'s, and as such I have only been able to execute my command successfully as such:



sed -e "s/\(.*$MATCH\)/#\1/g" list.txt > list.txt.1
The issue with this is that if I choose to write to my input file (index.txt), the end result is an empty file. I have tried >> in place, and I end up with the original file contents plus their replacements.

Hopefully there is something obvious I'm missing, but regardless, can anyone shed some light on the situation?
Old 09-03-2009, 03:36 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You need the -i option of sed to edit the file in place. Use -i.bck if you want to do a backup copy of the original file. The suffix .bck is customizable.
sed -i.bck ...
The reason for which you get an empty file is that the > redirection creates the output file before executing the command, so first it overwrites the original file with an empty one, than executed the command which results in nothing since the input at this point is totally null.
Old 09-03-2009, 03:45 AM   #3
LQ Newbie
Registered: Nov 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Your explanation cleared things up perfectly - thanks for such a quick reply!

I had a hunch I was missing something purely BASH related, but my shell scripting knowledge still has a lot of room for expansion.


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
Problems with a substitution using sed wtaicken Programming 4 12-15-2008 04:04 AM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
sed substitution with p flag 7stud Linux - Newbie 2 03-03-2007 04:15 AM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 PM.

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