Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
$
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!
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
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.