get characters from output of a command in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get characters from output of a command in a variable
# 1  
Old 06-02-2008
get characters from output of a command in a variable

Hi,

i have two questions, I am new to programming
1. I have an output of a command and i want to get some specific part of it in a variable. i am trying

sr=`some comand xyz| grep 'Last Changed Rev:' | cut -c19-`
now variable sr gets a end of line character at end.

output of the command is

Date: yyyy-mm-dd
---------

abc
xyz
Last Changed Rev: 10
abc
-------------


i want to get this 10 in the variable sr. i dont know the number length in advance.

2. Also the comand returns multiple lines output, how to get complete text in between

abc
xyz
Last Changed Rev: 10
abc


in another variable?
please help!

Thanks in advance..

Last edited by muaz; 06-02-2008 at 09:18 AM..
# 2  
Old 06-02-2008
To get only the "10" from that kind of line into a variable (and it doesn't matter how long this number will become because we only fetch the last field):

With awk:
Code:
VAR=`awk '/^Last Changed Rev:/ {print $NF}' nameofoutputfile`

With sed:
Code:
VAR=`sed 's/^Last Changed Rev: *\([^ ]*\)$/\1/g' nameofoutputfile`


To cut off the header let's use awk again. Let's say the data you want starts at line number 3:
Code:
cat nameofoutputfile | awk 'NR > 2 {print}'

# 3  
Old 06-02-2008
zaxxon preceded me Smilie However:

Code:
text=`some comand xyz | sed '/abc/,/abc/!d'`

sr=`echo "$text" | awk '/Last Changed Rev:/ { print $NF }'`

# 4  
Old 06-02-2008
thanks , its is done Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with making the output of a command a variable

I'm writing a script that goes something like this: #!/bin/bash zenity --list --checklist --title="Choose Packages to Install" --width="1000" --height="400" \ --column="Select" --column="Package Name" --column="Description" \ GIMP=$( " " GIMP "GIMP is a free and open source photo editor." ... (1 Reply)
Discussion started by: Defunct_Lizard
1 Replies

2. Shell Programming and Scripting

How to get the output of a ISQL command in a variable?

I am trying to run a query which returns a sum value(a number). I want to get it in a variable so that i can refer to that variable in different places. when i am running the following command variable=`isql -Uuser -Sserver -Ppassword 1> select sum(count(*)) from xyz..abc where clm_id... (2 Replies)
Discussion started by: Sharma331
2 Replies

3. UNIX for Dummies Questions & Answers

Need to get a loop variable from output of a command

Hi, I am trying to get a loop counter i and set its value as the ouput of a command: i=`printmo TEST1 | grep -i TEST2 | wc -l` Then I want to use i as counter to run a loop i number of times. Like if i gets a value of 5 I'll have to run loop 5 times. But will i here be a numeric... (3 Replies)
Discussion started by: pat_pramod
3 Replies

4. Shell Programming and Scripting

set variable to command output

I'm hoping you guys can help me out here. I've been trying different methods to try and get what IW as hoping would be a fairly simple script but has turned into a pain. Bit of background - I am writing a script to check values in certain failes to ensure they are corerct. I'm runnign this on... (2 Replies)
Discussion started by: stuc
2 Replies

5. Shell Programming and Scripting

How to put output of one command into a variable

Hi, Let say I have these 3 files (state, list and myscript). I want to be able get the sample output like below when I run myscript. Any one know how to fix the code? TIA. ~~~~~~~~~~~~~~~ > cat /home/state CA > cat /home/list CA 100 50 20 AUS 120 61 10 > cat myscript... (6 Replies)
Discussion started by: joker_789us
6 Replies

6. Programming

Command output into a variable

Hi, with this command: cu -l /dev/ttyACM0 -s 9600 > name.txt I put the output of the port in a txt Is posible to do the same (or similar) in a var directly, inside a C program? cu -l /dev/ttyACM0 -s 9600 > variable ? I have trying this withs pipes, but i dont know how to... (6 Replies)
Discussion started by: daaran
6 Replies

7. UNIX for Dummies Questions & Answers

saving command output to a variable

Hello, I have a shell script containing a command string in the following format: command1 | command2 | cut -c9-16 The output from this is a record number (using characters 9-16 of the original output string) e.g. ORD-1234 I wish to save this value to a variable for use in later commands... (4 Replies)
Discussion started by: philjo
4 Replies

8. UNIX for Dummies Questions & Answers

ls command output to variable in script

Hi, I wrote a script to get the oldest file from a directory path (which is passed as a parameter to the script) ######################################################### XMLFILE_PATH={$1} cd $XMLFILE_PATH JPM_FILENAME = `(ls -tr User* | head -1)` #echo $JPM_FILENAME ###### END... (1 Reply)
Discussion started by: dsrookie
1 Replies

9. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies

10. Shell Programming and Scripting

Command output to a variable.

With cut -c 8-13 myfile, I am getting some numeric value. In my shell script I am trying to assign something like this, var=cut -c 8-13 myfile But at the time of execution I am getting -c is not found. If I dont assign, then script executes well. Can we not simply use the value from one... (8 Replies)
Discussion started by: videsh77
8 Replies
Login or Register to Ask a Question