LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 10-08-2008, 10:40 PM   #1
wilsun
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Rep: Reputation: 0
Help with sed


Hi. I am currently working on a script, but have encountered some difficulty with sed. The said line is as follows:

Code:
#!/bin/bash
...
...
	sed "s/^($oname)\(.*\)$/($nname)\1/" tempm >edited
...
...
The purpose of this line is to look through the text file tempm, which contains only one line of text at a given time, and if it begins with a pattern that matches exactly the variable oname, replace that portion with the variable nname. Both oname and nname are strings that consists of a single word (i.e. oname could be ab and nname could be bc) Finally, the edited version of the text would be saved into a new file called edited.
Unfortunately, this line does not function correctly. tempm, which I know begins with what is in oname, is not edited into a desired string, and thus the file edited is just the same as tempm.
So, if you guys can figure out what's wrong, thanks in advance.

Note: I am really a newb when it comes to shell. If you do find what's wrong, explanation would be greatly appreciated.
 
Old 10-08-2008, 10:47 PM   #2
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
What's wrong with:
Code:
$ export FOO=in
$ export BAR=out
$ echo "input" | sed s/^$FOO/$BAR/ 
output
 
Old 10-08-2008, 11:13 PM   #3
wilsun
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
I don't know. I tried it, but it produced the same result.
 
Old 10-08-2008, 11:31 PM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
test.sh
Code:
#!/bin/sh

FILE=tempm
OUT=edited

PATTERN=test
SUBST=new

cat $FILE | sed "s/^$PATTERN/$SUBST/g" > $OUT
tempm
Code:
testout
$ ./test.sh
$ cat edited
newout
 
Old 10-09-2008, 12:44 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Your sed command works for me. Today I tried to use comm to compare two lists of IP addresses. They all turned out to be unique. The problem was that one of them had "\n\r" endings. I could have sworn that I used dos2unix on both files.

Check if something like that is the case with your file. There could be a first character on a line that you can't see. Look at the file with "cat -A <filename>" and "od -c <filename>".
 
Old 10-09-2008, 01:05 AM   #6
wilsun
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Actually, I found out what is wrong with my script. I changed bash to sh and everything worked. Am I just really newbish?
 
Old 10-09-2008, 01:16 AM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Nothing in the script above indicates there would be an error; a change in bash to sh may avoid certain expansions unique to bash. But we're left guessing as we really don't see your actual input data nor the run results of the script segment.

If this is of interest to you, show the exact input line, and output from your running the input through your sed command using the two shells. Remove the redirection to ease showing of output.
 
Old 10-09-2008, 01:44 AM   #8
wilsun
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Here is what I ended up using:

Code:
for i in $(grep ^$pat1 files)
    do
	echo $i | sed "s/^$pat1/$pat2/g" >edited
        ...
        ...
    done
In this case, I am looking for entries in the file files that begin with the pattern of $pat1, and replace that with $pat2.

In the end,
Code:
$ ls
  stuff stuff2 stuff3 pat1fafa pat1lala
$ script pat1 pat2
$ ls
$ stuff stuff2 stuff3 pat2fafa pat2lala
I'm afraid I couldn't give out any more details since this is for a school assignment and more details might be seen as plagiarism. If you truely are interested, PM me and I'll be happy to supply you with what I have.
 
Old 10-09-2008, 01:55 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Be sure to quote any variables that when interpolated may result in shell meta-characters, such as:

Code:
for i in $(grep ^$pat1 files)
where if $pat1 expands to "foo bar" you end up with:
Code:
for i in $(grep ^foo bar files)
which is probably not what you want.

I don't know how to reconcile your statement that you switched to sh, and yet are using the bash command expansion construct in the for loop above.

Its not clear to me how you asking for clarification could constituted plagiarism, unless you are worried someone else from your class would copy your work, and bring you down as well. I'll reverse your offer - you can email me if you want clarification on why your assignment is not working, if you are interested. :-)
 
Old 10-09-2008, 04:27 AM   #10
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,614

Rep: Reputation: 136Reputation: 136
Is there any advantage to use a loop here, I mean why not just do:

sed -e "s/^$pat1/$pat2/g" files > edited
 
  


Reply

Tags
sed


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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
using sed ilnli Linux - General 3 07-01-2005 03:59 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


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