LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-28-2013, 03:41 PM   #1
lolulove
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
Using sed with variable from a file


Am trying to replace every occurence of lines from today1.txt in today2.txt with space and write the output to today3.txt.

have tried this:

for i in `cat today1.txt`
do
sed "s/$i//g" "today2.txt" > today3.txt

but not working.
for example: if today1.txt contains:
1
2
3
4
and today2.txt contains:
1
3
5
the result today3.txt should only contain (two blank lines and the third line should be 5)


5
Kindly assist

Last edited by lolulove; 05-28-2013 at 03:59 PM.
 
Old 05-28-2013, 09:15 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With InFile1 ...
Code:
1
2
3
4
... and InFile2 ...
Code:
1
3
5
... this code ...
Code:
comm -3 $InFile1 $InFile2  \
|sed 's/$/\t/'             \
|cut -f2                   \
> $OutFile
... produced this OutFile ...
Code:
(blank line)
(blank line)
5
Daniel B. Martin
 
Old 05-29-2013, 10:57 AM   #3
lolulove
LQ Newbie
 
Registered: May 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
thank you. its works. God bless you
 
Old 05-30-2013, 05:25 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.

2) Don't Read Lines With For! Always use a while+read loop when taking input from a file or other command. It's a Useless Use Of Cat as well.

3) When you do have to use command substitution, $(..) is highly recommended over `..`.

4) The logic of the loop is wrong for the purpose described anyway. The '>' redirect will overwrite today3.txt on each iteration of the loop, meaning you'll only ever see the last entry in the final output.

What you'd need to do is re-feed the result of each iteration back into sed for further processing. You can use sed's -i option to edit it in place.


Code:
cp today2.txt today3.txt

while read -r var ; do

    sed -i "s/$var//g" today3.txt

done <today1.txt
But all this is rather inefficient, since you have to run one sed command for every entry. I'd probably do something like this instead.

Code:
sed -f <( sed 's|.*|s/&//g|' today1.txt ) today2.txt >today3.txt
The sed embedded in the process substitution converts the contents of today1.txt into a series of sed expressions. This is then passed to the main sed process through the -f "read expressions from a file" argument, which uses it to replace every matching line in today2.txt, with the output going to today3.txt.
 
1 members found this post helpful.
  


Reply



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
[SOLVED] Using a variable in sed to pick a specific line from a text file, bash Sunvic Linux - Newbie 10 08-12-2012 11:03 AM
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-27-2011 11:13 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
how-to make sed read 1 random line into a file and parse it ot a variable?? Speedy2k Linux - Newbie 7 05-24-2009 11:23 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration