LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-20-2008, 06:03 PM   #1
Lemmings
LQ Newbie
 
Registered: May 2008
Posts: 7

Rep: Reputation: 0
Problems with a simple variable definition


Hello,

I have a simple problem I cannot figure out. I would like to put the output of

echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/

which is

NA21322,CEU.CEL

into a new variable, call it NEWFILE. However, using

NEWFILE='echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/'

does not work. Since then

$NEWFILE

gives

CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/

How do I get this to work?
 
Old 05-20-2008, 06:14 PM   #2
vadkutya
Member
 
Registered: Apr 2008
Distribution: slackware 10.2
Posts: 117

Rep: Reputation: 16
try it with `...`:
Code:
NEWFILE='echo `CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/`'
not sure though

vadkutya

Last edited by vadkutya; 05-20-2008 at 06:16 PM.
 
Old 05-20-2008, 06:48 PM   #3
fbianconi
Member
 
Registered: Apr 2008
Location: argentina
Distribution: Arch
Posts: 86

Rep: Reputation: 22
Code:
NEWFILE=`CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/`
will do it.

NEWFILE='echo `CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/`'
will give you 'echo NA21322,CEU.CEL'
which is not what you mean.
the other forma I know is to put it like this
NEWFILE=$(CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/)

for future doubts about bash refer to man pages of bash
'man bash'
 
Old 05-20-2008, 09:07 PM   #4
Lemmings
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by fbianconi View Post
Code:
NEWFILE=`CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/`
will do it.

NEWFILE='echo `CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/`'
will give you 'echo NA21322,CEU.CEL'
which is not what you mean.
the other forma I know is to put it like this
NEWFILE=$(CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/)

for future doubts about bash refer to man pages of bash
'man bash'
Thanks for the help. Although what you guys have suggested doesn't quite work but I figured it out from your suggestions. It needs to be:

NEWFILE=$(echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/)

Otherwise it still tries to interpret CEU_NA21322_NSP.CEL as a command when in fact it is a file.
 
Old 05-20-2008, 09:15 PM   #5
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Hey There,

It appears to be okay. I'm not sure what you mean about this being interpreted as a command? My output (using your solution) below:

Quote:
-bash-3.2$ NEWFILE=$(echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/)
-bash-3.2$ echo $NEWFILE
NA21322,CEU.CEL

Best wishes,

Mike
 
Old 05-21-2008, 06:06 AM   #6
vadkutya
Member
 
Registered: Apr 2008
Distribution: slackware 10.2
Posts: 117

Rep: Reputation: 16
hey Lemmings,

thanx for the correction. however if this works:
Code:
NEWFILE=$(echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/)
then this:
Code:
NEWFILE=`echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/`
should also work since $(x) is the same as `x`. but quotes in bash are PITA. but in think that $() is preferable because it's easier to read.

cheers, vadkutya
 
Old 05-21-2008, 10:53 AM   #7
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Yeah,

The $() is more convenient because it removes a lot of the hassles involved with escaping special characters (like \` or whatnot) inside your command chain).

Good show,

Mike
 
  


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
simple variable problem that makes me head hurts :] itz2000 Linux - Newbie 4 11-19-2006 12:13 PM
Simple quick one - how to set a variable from a text file... stardotstar Programming 21 07-23-2006 05:47 AM
simple bash variable command itz2000 Programming 6 06-19-2006 09:24 PM
environment variable definition skwattakamp Fedora 4 07-24-2004 12:17 AM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 06:11 AM


All times are GMT -5. The time now is 03:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration