LinuxQuestions.org
Visit Jeremy's Blog.
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 05-27-2009, 12:51 PM   #1
figo
LQ Newbie
 
Registered: May 2009
Posts: 14

Rep: Reputation: 0
Smile sed output -> variable problem


I want to put the output of sed -n '/something/'= response.txt to a variable, say line_number.

But I try the following and it won't work in bash:

set line_num = $(sed -n '/something/'= response.txt )
echo $line_num


Any thoughts? Thanks
 
Old 05-27-2009, 02:11 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
You have to tell us what exactly happens, doesn't work, etc. Just saying "it won't work" tells us nothing.

First, get a sed command that produces an output. Then add the code to assign that output to a variable. In your example, the "=" before response.txt is not legal syntax.

For example, this works:
Code:
mherring@hplap:~$ cat list
1.2.3.4
5.6.7.8
4.6.8.0
2.3.4.5
5.4.3.2

mherring@hplap:~$ var=$(sed -n '1,2p' list)
mherring@hplap:~$ echo $var
1.2.3.4 5.6.7.8
mherring@hplap:~$
Keep in mind that "sed -n" produces no output unless it matches the specified conditions.
 
Old 05-27-2009, 02:23 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

the syntax of your command is wrong:
Code:
jan@jack:~/tmp> line_num=$(sed -n '/perl/=' test.pl)
jan@jack:~/tmp> echo $line_num
1
Jan
 
Old 05-27-2009, 11:02 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by pixellany View Post
In your example, the "=" before response.txt is not legal syntax.
I think you mean the spaces around the = are not legal (technically, they are syntactically legal, they just change the semantics from the assignment construct to a command whose first argument is =).

Also, you do not want to use the set command.
 
Old 05-28-2009, 12:40 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Re 'set' cmd; I thought that was compulsory in cshell?
Never used cshell myself, luckily.
 
Old 05-28-2009, 09:00 AM   #6
figo
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
[QUOTE=pixellany;3554637]You have to tell us what exactly happens, doesn't work, etc. Just saying "it won't work" tells us nothing.

First, get a sed command that produces an output. Then add the code to assign that output to a variable. In your example, the "=" before response.txt is not legal syntax.

I use "=" to output the line number, not to assign a value
 
Old 05-29-2009, 07:58 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by chrism01 View Post
Re 'set' cmd; I thought that was compulsory in cshell?
Yes it is (but the OP is using bash).

In C Shell, it is completely opposite: the extra spaces are required (as well as the set command).
 
Old 05-29-2009, 10:49 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
[QUOTE=figo;3555417]
Quote:
Originally Posted by pixellany View Post
You have to tell us what exactly happens, doesn't work, etc. Just saying "it won't work" tells us nothing.

First, get a sed command that produces an output. Then add the code to assign that output to a variable. In your example, the "=" before response.txt is not legal syntax.

I use "=" to output the line number, not to assign a value
<<Edit: Remove reference to incorrect statement---see later posts discussing the syntax>>>

Regardless, do you have it working now?

Last edited by pixellany; 05-31-2009 at 07:46 AM. Reason: removed error
 
Old 05-30-2009, 01:58 AM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by pixellany View Post
I think someone already pointed out that the "=" has to be inside the quotes containing the sed logic.
If they had, they would have been lying.
 
Old 05-30-2009, 01:28 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by osor View Post
If they had, they would have been lying.
Hmmm...I think the syntax has some ambiguities. I started to run some tests and the answer has not jumped out at me.
 
Old 05-30-2009, 05:06 PM   #11
figo
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by pixellany View Post
Hmmm...I think the syntax has some ambiguities. I started to run some tests and the answer has not jumped out at me.
The symbol '=' for line number has to be out of the Quote, otherwise there will be errors.

The original syntax should actually work just fine in tcsh (I've not tried yet), what I ignored it's that I'm actually using the expect, rather than simply tcsh, so the syntax should comply with the tcl, the syntax of which can be found over here. http://www.tcl.tk/man/tcl8.5/tutorial/Tcl2.html

In tcl, the way to sign the value is simply: set X 123

for my original command, it should be like: set line_num {sed -n '/something/'= response.txt} The pair {} is needed here to tell the execution environment that what inside the {} should be treated as a whole.

So thanks for you guys help anyway

Last edited by figo; 05-30-2009 at 05:11 PM.
 
Old 05-30-2009, 09:56 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by figo View Post
The symbol '=' for line number has to be out of the Quote, otherwise there will be errors.
Not in bash (which is what you initially said you were using). In bash and bourne-compatible shell grammar, = is not an considered operator, so the following is tokenized identically:
Code:
line_num=$(sed -n '/something/=' response.txt)
and
Code:
line_num=$(sed -n '/something/'= response.txt)
 
Old 05-31-2009, 08:01 AM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I now realize that I was being dense....

The trick is to first construct the logic without quotes (single or double). If any character in the logic string has meaning to BASH (as opposed to meaning to SED), the quotes have to be added to that character.

This is why the SED books suggest always starting with single quotes.
 
Old 05-31-2009, 09:26 AM   #14
figo
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osor View Post
Not in bash (which is what you initially said you were using). In bash and bourne-compatible shell grammar, = is not an considered operator, so the following is tokenized identically:
Code:
line_num=$(sed -n '/something/=' response.txt)
and
Code:
line_num=$(sed -n '/something/'= response.txt)
Thanks for your insightful suggestion!
 
  


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
Problem using sed with variable created using xargs - unterminated error wrankin Programming 5 11-14-2011 07:53 PM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM


All times are GMT -5. The time now is 09:11 AM.

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