LinuxQuestions.org
Register a domain and help support LQ
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 09-03-2009, 03:32 AM   #1
bezeek
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:

Code:
list.txt

1/user1/myuser1@foo.bar
2/user2/myuser2@foo.bar
etc...
Code:
commenter.sh

...
$USER=user2
$EMAIL=myuser2@foo.bar
MATCH=$USER.$EMAIL
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
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
Code:
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
bezeek
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.
 
  


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
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


All times are GMT -5. The time now is 07:21 PM.

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