Trim sed output & assign to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim sed output & assign to variable
# 1  
Old 03-12-2013
Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces.

My requirement is to trim the spaces around the value and assign to a variable.

Code:
 
sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml

How do I go about it?
# 2  
Old 03-12-2013
Here is how you assign output to a variable.
Code:
var=$(sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml)

If you post the input and how you like the output would be, it would be more easy to give you a correct guide.
This User Gave Thanks to Jotne For This Post:
# 3  
Old 03-12-2013
Using awk to remove leading and trailing spaces:
Code:
awk -F'[<>]' '/<port>/{ gsub(/^[ \t]+|[ \t]+$/,x,$3); print $3 } ' cfg.xml

# 4  
Old 03-12-2013
Just add a space before and after your parenthesis:

Code:
sed -n 's|<port> \(.*\) </port>|\1|p' file
8900

:
# 5  
Old 03-12-2013
Thank you variable assignment worked.

cfg.xml has XML as below....

Code:
 
<server>
........
........
<port>7001</port>
........
........
</server>

Code:
 
var=$(sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml)

O/P:" 7001" (quotes included here for showing space. not part of output)

Expected O/p:"7001"

---------- Post updated at 03:24 PM ---------- Previous update was at 03:17 PM ----------

How do I use awk in conjunction with sed?

My true requirement is to retrieve the value of an xml node that may or may not contain spaces.

the sed code, in my case returns value with spaces even though the XML does not contain spaces.



Quote:
Originally Posted by bipinajith
Using awk to remove leading and trailing spaces:
Code:
awk -F'[<>]' '/<port>/{ gsub(/^[ \t]+|[ \t]+$/,x,$3); print $3 } ' cfg.xml

# 6  
Old 03-12-2013
Code:
sed -n 's|.*<port> *\(.*[^ ]\) *</port>.*|\1|p' ../cfg.xm

Narrative: If you find a line with '<PORT>', optional spaces, some not-space-value, optional spaces and '</port>', turn the entire line into the not-space-value and print it. The * is greedy, so the ' * after <port> will stop just before the first not space value, and we must stop the captured \(...\) value before we scoop up any spaces, so it must end in a non-space; the .* before that is ensured to start with a not space by the greedy prior ' *' -- left greedy beats right greedy. Not that I ensure anything on the line before and after is picked up and not laid back down. We could still be fooled by '<port> 1 2 </port>', but we are assuming that the data is 'well behaved' with one port on any line. If it was really well behaved, there would be no spaces in there. Maybe they were before or after it all, not inside. Never give a white space your trust, it could be spaces, backspaces, tabs, carriage returns, form feeds, iso8859-1 nonbreaking spaces (space + 128), and other control characters that leave no glyph. In ascii, there is [!-~] that you can see, and then there is the rest.

Last edited by DGPickett; 03-12-2013 at 04:39 PM.. Reason: I only wanted one space there. Two spaces is for speed in other space chasers.
This User Gave Thanks to DGPickett For This Post:
# 7  
Old 03-12-2013
Thanks for the code and narrative.

I tried the code but nothing was returned. No error. No output.


Quote:
Originally Posted by DGPickett
Code:
sed -n 's|.*<port>  *\(.*[^ ]\) *</port>.*|\1|p' ../cfg.xm

Narrative: If you find a line with '<PORT>', optional spaces, some not space value, optional spaces and '</port>', turn the entire line into the not space value and print it.
---------- Post updated at 03:31 PM ---------- Previous update was at 03:28 PM ----------

Tried with a single space after the <port> and it worked. Spaces trimmed and output assigned. Thanks much!

Code:
 
sed -n 's|.*<port>  *\(.*[^ ]\) *</port>.*|\1|p' ../cfg.xm

Quote:
Originally Posted by DGPickett
Code:
sed -n 's|.*<port>  *\(.*[^ ]\) *</port>.*|\1|p' ../cfg.xm

Narrative: If you find a line with '<PORT>', optional spaces, some not space value, optional spaces and '</port>', turn the entire line into the not space value and print it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Assign the Output of an SQL Query to a Variable?

Hi iam new to shell scripting how to declare variables as redshift query and I have to compare two counts by using if condition . ex:count=select count(*) from prd; select count(*) from prd; select count(*) from tag; can any one help me . Please use CODE tags when displaying... (1 Reply)
Discussion started by: sam526
1 Replies

2. Shell Programming and Scripting

Assign output to dynamic variable

Hi Folks, I am trying to assign a value from the command to a dynamic variable. But I am not getting the desired output.. I am sure something is wrong so i need experts advise. There will be multiple files like /var/tmp/server_1, /var/tmp/server_2, /var/tmp/server_3, having different server... (6 Replies)
Discussion started by: ganga.dharan
6 Replies

3. Shell Programming and Scripting

Unable to assign command output to variable

Code set -x STATUS="0" echo $STATUS for i in `ls -ltr Report*|awk '{ print $9 }'` do if then flg = "`head -1 "$i" |cut -c 31-33`" echo `head -1 "$i" |cut -c 31-33` echo $flg if then echo "having Fun" STATUS="2" else echo "no Fun" fi fi (2 Replies)
Discussion started by: Funkeydude
2 Replies

4. Shell Programming and Scripting

Assign dscl output to variable as an array

Greetings folks, I am trying to assign the output of a dscl command (contains name<spaces>id) to a variable as an array. Currently I am piping the output into a tmp file, then reading the tmp file into an array, then parsing the array. I would like to bypass creating the tmp file portion of... (6 Replies)
Discussion started by: macnetdaemon
6 Replies

5. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

6. Shell Programming and Scripting

read contents of a file with serveral lines & assign it to a variable

Hi All I have a file for ex .log file which contain several lines within it. I have to read that file contents & assing that to a variable. (2 Replies)
Discussion started by: satyam.sumit
2 Replies

7. Shell Programming and Scripting

To get value from oracle & assign it in UNIX variable

Hi Team, I need to get data from oracle table & need to assign that value to unix variable. I have serched the same in other threads. I found the following code. I have tried code to get the value from oracle. but it is not working. The error shows invalid identifier "NAM" & then list all... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

8. Shell Programming and Scripting

hot to assign output to a variable

I want to assign a comment to a veriable for example my program head -1 myfile I want to assıgn output to a variable (1 Reply)
Discussion started by: walnut
1 Replies

9. UNIX for Dummies Questions & Answers

how to assign an output to a variable

Hi, I am giving a grep command, and i am getting the output. i want to store it in a variable for eg a = grep '12345' /dir/1/2/log.txt ( the output is number) b= grep 'basic' /dir/1/2/log1.txt (in this case the output is character) so how to assign the output of grep to a variable ... (1 Reply)
Discussion started by: vasikaran
1 Replies

10. Shell Programming and Scripting

How to trim space in output variable ?

Hi , I have a code like this: uid=scott password=tiger database=db01 cat >runid_val.sql<<-EOA SET ECHO OFF SET FEEDBACK OFF SET HEADING OFF SELECT trim(runid_seq.nextval) FROM dual; EXIT EOA echo `cat runid_val.sql` V_RUNID=`sqlplus -s $uid/$password@$database @runid_val.sql`... (5 Replies)
Discussion started by: vj_76
5 Replies
Login or Register to Ask a Question