LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-02-2008, 07:34 PM   #1
mogra
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
Shell Script , using Sed


Hi Everybody,
This is my first post. I am relatively new to shell scripts ..
I have following shell script :

#!/bin/tcsh

foreach x (*topRank.txt)
echo `basename $x .txt `
set PRE=`basename $x .txt`
cut -f 1 $x | cut -d ' ' -f1 | sed 's/NAME/$PRE/' | sed 's/VC0/VC_0/' | tr "\n" "\t" >> all.log
echo >> all.log #inserts new line
end

Overall Goal : For each file in the the current directory which ends with topRank.txt do something
a) cut the first column of the file than further again cut the column by using space a deliminator, now replace NAME with the either just prefix of the file name or may be $x , it doesnt matter as long as the it is I know some how that this line come from this file id.
b) replace few things
c) now put everything on a single line with tr cmd

Everything works ...except following.


Problems :

a) sed 's/NAME/$PRE/' Or sed 's/NAME/$x/' does not work it replace NAME with $PRE and $x respectively instead of the file name.

Thanks of help.


If this the wrong forum to post this kind of question please direct me to the right forum.

Thanks a lot.
 
Old 03-02-2008, 07:43 PM   #2
idoneous
LQ Newbie
 
Registered: Jul 2006
Location: York, PA
Distribution: Linux Mint 14
Posts: 27

Rep: Reputation: 0
mogra,
Just did a quick check from the command line. Try using single quotes to escape out of sed, so bash can replace your variable ( the file name ) with the current value.
eg:
$x=foo
$sed 's/NAME/'$x'/'
NAME Tom
foo Tom
$

Hope that helps!
Tom
 
Old 03-02-2008, 11:46 PM   #3
mogra
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks a lot.

It worked
 
Old 03-04-2008, 05:04 PM   #4
idoneous
LQ Newbie
 
Registered: Jul 2006
Location: York, PA
Distribution: Linux Mint 14
Posts: 27

Rep: Reputation: 0
Glad to help. There are a ton of good bash scripting sites out there, too. Google around when you get stuck. I always keep a copy of "The Unix Programming Environment" by Kernighan and Pike handy, too. Old and outdated? Yes, but it's a gem by the guys who gave us Unix. The first half is all about the shell.
Happy Hacking!
 
Old 05-03-2008, 12:49 PM   #5
Asy
LQ Newbie
 
Registered: May 2008
Location: The Netherlands
Distribution: Ubuntu
Posts: 25

Rep: Reputation: 16
Hi,

The problem are the single quote's. Because of using those singe quote's your parameter $x won't be substitute.

A nice solution will be this one:

$sed "s/NAME/$x/"

Regards

Asy
 
Old 05-03-2008, 09:05 PM   #6
anon099
Member
 
Registered: Oct 2002
Posts: 188

Rep: Reputation: Disabled
haha... y'all totally did this kids homework for him. hahahaha!
 
Old 05-06-2008, 08:27 PM   #7
idoneous
LQ Newbie
 
Registered: Jul 2006
Location: York, PA
Distribution: Linux Mint 14
Posts: 27

Rep: Reputation: 0
Unclear What You Mean

Asy,
Not sure quite what you mean. The script I posted does replace the variable. Follow the quotes:
$sed 's/NAME/'$x'/' The single quote after the slash ends the first set of single quotes. The second set of single quotes begins after the variable $x. That's the whole idea. I haven't checked your method, but my variant ensures that any double quotes in the NAME won't interfere.
I'm more than willing to be wrong, so let me know if I missed something. ( I'm pretty new to Linux but old to Unix ).
Thanks!
Idoneous
 
Old 05-06-2008, 08:35 PM   #8
idoneous
LQ Newbie
 
Registered: Jul 2006
Location: York, PA
Distribution: Linux Mint 14
Posts: 27

Rep: Reputation: 0
ooops

Hate to post on my own post, but just caught that mogra is using tcsh. I have no experience with that shell. I have only used sh, ash, and bash. I don't know if that's what you were referring to, Asy. It seems to have worked, though.

Padlamoij - I was waiting for a reply to a post of my own, so I didn't mind helping someone else out. That's what the forum is for! I wish the internet had been available when I was in school. All we had was bitnet.

-Id
 
Old 05-07-2008, 01:18 PM   #9
Asy
LQ Newbie
 
Registered: May 2008
Location: The Netherlands
Distribution: Ubuntu
Posts: 25

Rep: Reputation: 16
Hi idoneous

Quote:
Originally Posted by idoneous View Post
I haven't checked your method, but my variant ensures that any double quotes in the NAME won't interfere.
I'm more than willing to be wrong, so let me know if I missed something. ( I'm pretty new to Linux but old to Unix ).
It isn't nessassery to chech I didn't neighter.
I struggeld a lot with those stuff, you solution will also work! For myself my solution is more readable,
It is the same to me:

Quote:
Originally Posted by idoneous View Post
( I'm pretty new to Linux but old to Unix ).
I have a lot of experience with HP-UX.
Asy

Last edited by Asy; 05-07-2008 at 01:22 PM.
 
  


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
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
Using sed in a shell script RobHill Linux - General 4 05-29-2007 03:31 PM
Sed shell script question pete1234 Programming 4 08-18-2006 09:15 PM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
shell script problem on sed. chooi Programming 3 02-10-2006 11:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:49 PM.

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