LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-17-2013, 02:03 AM   #1
rajachan
LQ Newbie
 
Registered: Jan 2013
Posts: 10

Rep: Reputation: Disabled
Exclamation save command output to variable


hi

how to store below command output in variable in ksh. i had 3 columns of output.

echo $line|sed '/^$/d' |sed '/,/d'|sed '/SQL/d'|sed '/-/d'|sed '/NAME/d'| cut -d " " -f1)
echo $line|sed '/^$/d' |sed '/,/d'|sed '/SQL/d'|sed '/-/d'|sed '/NAME/d'| cut -d " " -f2
echo $line|sed '/^$/d' |sed '/,/d'|sed '/SQL/d'|sed '/-/d'|sed '/NAME/d'| cut -d " " -f3
 
Old 01-17-2013, 02:11 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use command substitution, e.g.
Code:
var=$(command)
This syntax is preferred over the old one using backticks
Code:
var=`command`
but sometimes you require it for compatibility reasons with the old Bourne shell /bin/sh.
 
Old 01-17-2013, 02:41 AM   #3
rajachan
LQ Newbie
 
Registered: Jan 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Use command substitution, e.g.
Code:
var=$(command)
This syntax is preferred over the old one using backticks
Code:
var=`command`
but sometimes you require it for compatibility reasons with the old Bourne shell /bin/sh.
i tried using var.. but the output of $var is empty..

but actually i got output for the commands
 
Old 01-17-2013, 03:30 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
An example, if can be useful:

Code:
$> test=$(echo "are we aliens?" | awk '{print $3}')
$> echo $test
$> aliens?
 
1 members found this post helpful.
Old 01-18-2013, 07:48 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Please show us the contents of the "$line" variable, the exact commands you're using, and the output/error messages you're getting. We can't see your screen, so we have no idea what you're doing wrong.

Some more context on what you are trying to do would help too. Where is this input coming from, and what do you really need to do with it?

In short, give us more details. It may be that there are better ways to do what you want.


Those long chains of separate seds are rather ugly in any case. You should be able to easily condense them into fewer ones. In fact, we could probably just do something like this:

Code:
read var1 var2 var3 _ <<<"$( sed -r '/(^$|[,-]|SQL|NAME)/d' <<<"$line" )"
Note that the above assumes there's always only a single line of text left after all the (d)eletes, and that that line will have at least 3 fields (anything extra will go into the throw-away _ variable.

It's also possible that awk is a better choice, since it's field-based. Or one of the shell's built in string manipulations. But again, we need to see what the input looks like before saying for sure.

Last edited by David the H.; 01-18-2013 at 07:50 AM.
 
  


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
Output a C variable with a shell command paliga Programming 4 07-20-2010 06:10 AM
[solved]Can't save grep output into variable deansaliba Linux - General 6 02-04-2010 06:58 AM
how to format the output of a command and save it into a string variable gn00kie Programming 1 07-15-2006 03:04 AM
how to save output from a command to a variable? tomolesonjr Linux - Newbie 6 04-19-2006 03:04 PM
passing command output to variable Infernal211283 Linux - Newbie 12 01-11-2006 05:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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