Stripping of a symbol from string not working properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping of a symbol from string not working properly
# 1  
Old 03-29-2012
Stripping of a symbol from string not working properly

Hi All,

I used this code to strip-off $-symbol from string values.

Code:
a="$980"
b="897"
a=`echo "$a" | sed 's/$/ /g'`
b=`echo "$b" | sed 's/$/ /g'`
echo "$a"
echo "$b"

but this results in the output:
80 and 897

it works when i use
Code:
a='$987'
b='890'

But I cannot use single-quotes in my actual code as the variable values are extracted as strings and are not constant.

Is there any other command or any fix for this ?
I even used 'tr', even that works similar to sed .
# 2  
Old 03-29-2012
I do not understand the question. How exactly are the values that contain the $-sign assigned to the variables? Can you show us the code where that happens?

By the way a more straightforward way of removing a leading $-sign (if it is always leading when it occurs), would be
Code:
echo "${a#$}"

or
Code:
a=${a#$}

# 3  
Old 03-29-2012
At shell processing at least you need to escape the variable:
Code:
# a="$980"                     
# echo $a
80

Code:
# a="\$980"
# echo $a  
$980

# 4  
Old 03-29-2012
sed

Quote:
Originally Posted by angie1234
Hi All,

I used this code to strip-off $-symbol from string values.

Code:
a="$980"
b="897"
a=`echo "$a" | sed 's/$/ /g'`
b=`echo "$b" | sed 's/$/ /g'`
echo "$a"
echo "$b"

but this results in the output:
80 and 897

it works when i use
Code:
a='$987'
b='890'

But I cannot use single-quotes in my actual code as the variable values are extracted as strings and are not constant.

Is there any other command or any fix for this ?
I even used 'tr', even that works similar to sed .
Try the way Scrutinizer suggested.
The reason behind for sed cmd not working is,
$ - has special meaning in sed( almost in all regex). so you have to put \ before using the special meaning characters.

Code:
a="\$980"
a=`echo "$a" | sed 's/\$/ /g'`

Cheers,
RangaSmilie
# 5  
Old 03-29-2012
Hi Scrutinizer,

$-symbol might not always be the leading character. The string most of the time contains leading spaces.

And sorry I have just posted the code I was working on to test how it works.
This is how the $-values are assigned to the varaiables:
Code:
a=`echo "$line" | cut -f9  -d '"' | cut -c32-41`
a=`echo "$a" | sed 's/$/ /g'`

# 6  
Old 03-29-2012
And could you post one or more examples of a typical "$line" ?
# 7  
Old 03-29-2012
This is an example of $lines:
Code:
111"987"2010-11-21 12:05:41.000"18"A"123"4"2010-11-21 12:06:05.000"                 xxxxxxxxxxx      $62.99    ""
111"654"2010-11-21 12:05:41.000"19"B"456"4"2010-11-21 12:06:05.000"              xxxxxxxxxxxxxxx     $20.00    ""
111"321"2010-11-21 16:41:47.000"27"C"789"6"2010-11-21 16:42:37.000"                 xxxxxxxxxxxx     $20.97    ""

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expansion not working properly

I'm using an Ubuntu machine and expansion is not working properly. What would cause this? Do I need to check for any particular bash packages? $ ipcs -m | grep $USER | awk '{printf "%s ",$2}' $ ipcs -m | grep UNF | awk '{printf "%s ",$2}' 294912 1048577 425986 688131 786436 1245189... (14 Replies)
Discussion started by: cokedude
14 Replies

2. UNIX for Dummies Questions & Answers

~c is not working properly with -r option

Hi There, --------- file1 ------- ~c asd@ac.com -------------- Now i am using below command cat file1|mailx -s " testing" -r " My Name" abc@tech.com (3 Replies)
Discussion started by: Tapan Sharma
3 Replies

3. Linux

rexec not working properly

Hi, I am trying to enable rexec to automate certain tasks(it has to be rexec, not ssh or any other due to the system environment), so after switching to linux, I followed the certain instructions that were laid out in the web. My operating system is fedora 17, so I first installed the... (1 Reply)
Discussion started by: wringer
1 Replies

4. Shell Programming and Scripting

stripping out digits from a string with sed

i want to parse a string and only display the digits in that string... How would i accomplish this with sed command. For example. input string: " 033434343 dafasdf" output string: 03343434 Thanks (2 Replies)
Discussion started by: timmylita
2 Replies

5. Shell Programming and Scripting

\n not working properly

Hi all, I'm trying to generate a series of txt files starting from a plain csv file part of my code: #!/bin/ksh INSTALLDIR=/Users/ME/Installdir CSV=CSV.csv TMP=/tmp/$(basename $0).txt tr -s "\r" "\n" < /$INSTALLDIR/$CSV > $TMP function Makefiles { printf '%24s:%30s\n' "sometext"... (1 Reply)
Discussion started by: Jive Spector
1 Replies

6. Shell Programming and Scripting

stripping certain characters in at the middle of a string

I am trying to strip out certain characters from a string on both (left & right) sides. For example, line=see@hear|touch, i only want to echo the "hear" part. Well i have tried this approach: line=see@hear|touch templine=${line#*@} #removed "see@" echo ${templine%%\|*} #removed... (4 Replies)
Discussion started by: mcoblefias
4 Replies

7. Shell Programming and Scripting

Stripping the spaces in a string variable

Hi , i have to strip the spaces in the string which has the following value ABC DEF i want this to appear like this ABC DEF is there any spilt method? please help.... Thanks (3 Replies)
Discussion started by: rag84dec
3 Replies

8. Shell Programming and Scripting

stripping leftmost characters from string

Hi there, if i have some strings ie test_324423 test_242332 test_767667 but I only want the number part (the bolded bit) how do I strip the leftmost 5 characters from the output so that i will have just 324423 242332 767667 any help would be greatly appreciated Gary (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. UNIX for Dummies Questions & Answers

Stripping a portion of string from behind!!!

Hi, How to strip a portion of a file name from behind...Say for Eg..i have a file name like aaaaa.bbbbb.Mar-17-2007 i want to remove .Mar-17-2007...is there a one line command which can give this output... Thanks Kumar (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

10. Programming

y is this not working properly?

#include <stdio.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> struct stat s; main() { char c; if (fork()==0) { system("clear"); do { printf("myAI\\>§ "); scanf("%s",c); if(stat(c,&s)>-1) {... (3 Replies)
Discussion started by: C|[anti-trust]
3 Replies
Login or Register to Ask a Question