Removing leading spaces from the variable value.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing leading spaces from the variable value.
# 1  
Old 09-28-2009
Question Removing leading spaces from the variable value.

Hi All,

I am trying to replace the value of a xml tag with a new one. But, the existing value in the xml contain leading spaces and I tried to remove that with different sed commands but all in vain.

For replacing the value I wrote the command in BOLD letters below:

bash-3.00$ polnum=`grep "<PolNumber>" adi99.xml | sed 's:<PolNumber>::;s:</PolNumber>::' | sed 's/^ *\(.*\) *$/\1/'`
bash-3.00$ echo "PolNumber is:"$polnum
PolNumber is: 109301793
newpol=101101
bash-3.00$ echo "cat adi99.xml | sed -e 's/${polnum}/${newpol}/'"

cat adi99.xml | sed -e 's/"THERE ARE SPACES WHICH ARE GETTING REMOVED HERE ONCE I SAVE IT HERE" 109301793/101101/'


cat adi99.xml
<Holding id="Holding_1">
<Policy id="Policy_1">
<PolNumber>109301793</PolNumber>
</Policy>
</Holding>



Can you please suggest a way to remove these spaces.

Last edited by khedu; 09-28-2009 at 04:44 PM..
# 2  
Old 09-28-2009
bash might help you---

Code:
BASH:~> blah='  1234567'
BASH:~> echo "$blah"
  1234567
BASH:~> echo "${blah/ /}"
 1234567
BASH:~> echo "${blah// /}"
1234567
BASH:~>

${blah/ /} This will remove only the first space...
${blah// /} this will remove all spaces.
# 3  
Old 09-28-2009
Hi khedu,

I think you have remove all whitespace characters instead of just regular spaces, so instead of:
Code:
polnum=`grep "<PolNumber>" adi99.xml | sed 's:<PolNumber>::;s:</PolNumber>::' | sed 's/^ *\(.*\) *$/\1/'`

try:
Code:
polnum=`grep "<PolNumber>" adi99.xml  | sed 's:<PolNumber>::;s:</PolNumber>::'| sed 's/^[[:space:]]*\(.*\)[[:space:]]*$/\1/'`

or
Code:
polnum=`grep "<PolNumber>" adi99.xml  | sed 's:<PolNumber>[[:space:]]*\(.*\)[[:space:]]*</PolNumber>:\1:'`

or
Code:
polnum=$(sed -n 's:<PolNumber>[[:space:]]*\(.*\)[[:space:]]*</PolNumber>:\1:p adi99.xml)


Last edited by Scrutinizer; 09-28-2009 at 05:48 PM..
# 4  
Old 09-28-2009
no more grep, nor uuoc, nor extra spaces
Code:
$ polnum=$(sed -n '\:<PolNumber>:s:<PolNumber>\([^<]*\)</PolNumber>:\1:p' adi99.xml)
$ echo "PolNumber is:"$polnum
PolNumber is: 109301793
$ newpol=101101
$ sed 's/'"$polnum"'/'"$newpol"'/' adi99.xml
<Holding id="Holding_1">
<Policy id="Policy_1">
<PolNumber>101101</PolNumber>
</Policy>
</Holding>

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 remove leading and trailing spaces for variable in shell script?

Hi I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy ) INPUT ... (10 Replies)
Discussion started by: pottic
10 Replies

2. Shell Programming and Scripting

Trying to remove leading spaces

OS : RHEL 6.7 Shell : bash I am trying to remove the leading the spaces in the below file $ cat pattern2.txt hello1 hello2 hello3 hello4 Expected output is shown below. $ cat pattern2.txt hello1 hello2 hello3 hello4 (2 Replies)
Discussion started by: John K
2 Replies

3. UNIX for Dummies Questions & Answers

Need help removing leading spaces from one field in comma seperated file

Using awk or sed, I'd like to remove leading spaces after a comma and before a right justified number in field 6. Sounds simple but I can't find a solution. Each field's formatting must stay intact. Input: 40,123456-02,160,05/24/2012,02/13/1977, 10699.15,0 Output:... (5 Replies)
Discussion started by: Scottie1954
5 Replies

4. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

5. Shell Programming and Scripting

Not able to remove leading spaces

Hi Experts, In a file tht i copied from the web , i am not able to remove the leading white spaces. I tried the below , none of them working . I opened the file through vi to check for the special characters if any , but no such characters found. Your advice will be greatly appreciated. sed... (5 Replies)
Discussion started by: panyam
5 Replies

6. Shell Programming and Scripting

Removing leading and trailing spaces only in PERL

Hi All, I have a file with the following contents with multiple lines 172445957| 000005911|8| 400 Peninsula Ave.#1551 | And,K |935172445957|000005911 607573888 |000098536 | 2|Ane, B |J |Ane |1868 |19861206|20090106|20071001 I want to trim the "leading and trailing spaces only" from... (2 Replies)
Discussion started by: kumar04
2 Replies

7. Shell Programming and Scripting

Leading spaces of variable removed

#!/bin/bash TESTVAR=" 5spaces" echo $TESTVAR hostame:~# ./test.sh 5spaces The leading spaces from my variable are removed when the content is echo'd. I am trying to make some tabular data. `echo -e` also fails. Any suggestions? (2 Replies)
Discussion started by: grumm3t
2 Replies

8. Shell Programming and Scripting

Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros. (6 Replies)
Discussion started by: toshidas2000
6 Replies

9. UNIX for Dummies Questions & Answers

Removing leading and trailing spaces of data between the tags in xml.

I am having xml document as below. <transactionid> 00 </transactionid> <tracknumber> 0 </tracknumber> <key> N/A </key> But the data contains leading and trailing spaces between the tags. Please let me know how can i remove these leading and trailing spaces between the tags.... (2 Replies)
Discussion started by: jhmr7
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question