LinuxQuestions.org
Help answer threads with 0 replies.
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 06-27-2009, 07:04 AM   #1
Fixed_it
Member
 
Registered: Jun 2006
Location: New England
Distribution: FC11
Posts: 48

Rep: Reputation: 15
Question sed inputs from a file not working


I am having a little difficulty with sed, and am sure this is something simple- none the less...
I have a text file that I want to search replace: Template.txt
I have another text file that will supply the replace terms: Data.txt

My script, alter.sh is as follows:

...
sed -e "s/flag-1/$1" < Template.txt > $1.txt
...

This works if I enter the command :

alter.sh newflag

BUT if I use :

alter.sh < Data.txt

nothing happens. This was verified with a quick addition of echo on the line. the variable $1 is not defined when I use the file redirection. What am I doing wrong?
As always, thanks for any assistance.
 
Old 06-27-2009, 07:44 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
how does all your relevant file looks like? show examples of output also.
 
Old 06-27-2009, 09:42 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In your first example, you are passing an argument to the script. By convention, the first argument appears as $1, the second as $2, etc.

To use redirection, the script has to be reading---ie it has to be looking for input. The redirection simply says: "get it here instead of from stdin."
 
Old 06-27-2009, 10:14 AM   #4
Fixed_it
Member
 
Registered: Jun 2006
Location: New England
Distribution: FC11
Posts: 48

Original Poster
Rep: Reputation: 15
Hmm. That gives me a little to think about.

I only want one variable read, it is reused a few times in different places. Is what I have still appropriate?

Quote:
To use redirection, the script has to be reading---ie it has to be looking for input. The redirection simply says: "get it here instead of from stdin."
So sed is looking in Data.txt for the character strings instead of the input line (which points to Template.txt)? As I was trying to write the question up for this post I was wondering if that is what was happening. Excuse my ignorance here, but how would I write it so the script is looking for the input when the script is called? I know I could call the script and it can ask for the input file, but I would rather have the functionality resemble the way I call it in my previous post. I know this is pretty basic, but I just don't remember .
 
Old 06-27-2009, 12:02 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Do the experiments first in interactive mode on the terminal. eg. learn how the "read" command works.

Even simpler:
Code:
sed 's/pattern/newpattern/' < filename
Once this works as expected, then write this script, named "mysed":
Code:
#!/bin/bash
sed 's/pattern/newpattern/'
Make it executable, and then run it like this:
Code:
./mysed < filename
All should become clear..........
 
Old 06-27-2009, 04:57 PM   #6
Fixed_it
Member
 
Registered: Jun 2006
Location: New England
Distribution: FC11
Posts: 48

Original Poster
Rep: Reputation: 15
Pixellany, Thanks for the continued support!
I have actually tried your approach before posting the question, but have ended with no joy. I can get the cli command to work as intended, without the variable input in the command. I moved on to the shell script which also works, with the variable input, but not as I suspect:
This is the script:
Quote:
#!/bin/sh
#
#
#
echo "sed -e "s/input.dat/$1/"<Template.R>$1.R"
This is the result:
Quote:
[D@localhost Documents]$ ./custom.sh test
sed -e s/input.dat/test/<Template.R>test.R
[D@localhost Documents]$ ./custom.sh < data.lst
sed -e s/input.dat//<Template.R>.R
[D@localhost Documents]$
So when I put a single variable in my call to the script, it works (Oh, I forgot to say that I put in an echo at the start of the line for de-bugging). When I use the redirect, I get an empty variable. I ashamed to say I have spent hours of this trying to get it to work. I tried to distil my issues down to a single problem, and I think it is my understanding of the file redirection, but I could be wrong.
 
Old 06-28-2009, 05:01 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You can't use redirection to set the value of a variable (at least not directly).

In your example above, "custom.sh" is not expecting any input, so the redirection does nothing.

I assume that you are aware that the outer quotes is telling the echo command to output the literal string---If you wanted it to be the output of the sed command, it would be:
echo $(<command stuff>)
 
Old 06-28-2009, 08:19 AM   #8
Fixed_it
Member
 
Registered: Jun 2006
Location: New England
Distribution: FC11
Posts: 48

Original Poster
Rep: Reputation: 15
OK. Now I am getting somewhere. I understood that the echo was only returning the string, not the command, what you wrote would have been a little more helpful, but I don't think I knew it. I do now though!
So I can't use file redirection to set the variable value, can you suggest an efficient way to do this. The first thing that comes to my mind is to ask for input on the command line, but that seems hokey. I think I tried

Quote:
cat data.lst | custom.sh
But I don't think I got anything different from that either.
 
Old 06-28-2009, 08:59 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
"cat" with a pipe does exactly the same thing as redirection. It does not work because the script is not expecting an input!!

Back to basics:

1. You can pass variables (arguements) when calling a command (script). Inside the script they are named $1, $2, etc.

2. You can have the script read from a file:
A. Specified internally
B. Specified externally---using redirection or the pipe method you just showed.
Regardless of how you do it, the syntax in the script needs to get the data out of the file and into wherever you need it.

Go back to my earlier post and try the examples.....

Try this:
Code:
while read -p "enter a word: " item; do echo "enter a word"; echo $item; done <filename
Then try it without the "<filename"
 
Old 06-28-2009, 02:41 PM   #10
Fixed_it
Member
 
Registered: Jun 2006
Location: New England
Distribution: FC11
Posts: 48

Original Poster
Rep: Reputation: 15
Hahaha! Thanks for your patience- I actually tried your example the way I though it should (before I posted), and (as you can imagine) it didn't. That's why I though it was my understanding of the redirection. I get what you are saying now.

Thanks again!
 
  


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
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 04:34 PM
filesize limit on 'cat $file | sed > $file' drkstr Linux - Software 2 07-10-2006 02:47 AM
Sed command in file not working lbauer Programming 5 04-06-2005 12:31 PM

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

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