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
# 8  
Old 03-29-2012
Hi,

You could try this:
Code:
awk -F\" '{sub(/[^0-9]*/,x,$9);print $9}' infile

and if this file format is consistent, then you could perhaps use spaces a a field separator
Code:
a=$(printf "%s" "$line" | awk '{print $5}')
a=${a#$}

Or use probably more efficiently, use the read statement in the loop:
Code:
while read x x x x amount x
do
  a=${amount#$}
  echo "$a"
done < infile


Last edited by Scrutinizer; 03-29-2012 at 11:23 AM..
# 9  
Old 03-29-2012
Hi,

Can you please explain what is being done in the above three codes.
Thanks

Last edited by angie1234; 03-29-2012 at 10:24 AM..
# 10  
Old 03-29-2012
Or back to using "tr".
Code:
a=`echo "$line" | cut -f9  -d '"' | cut -c32-41 | tr '$' ' '`

This User Gave Thanks to methyl For This Post:
# 11  
Old 03-29-2012
Thanks Methyl !! This works fine.

But I need a clarification. Before, I have used 'tr' in two different expresions similar to 'sed' in the below code and it didn't work.
Code:
a=`echo "$line" | cut -f9  -d '"' | cut -c32-41`
a=`echo "$a" | sed 's/$/ /g'`

Does piping it into one single expression made the difference ? Or is there any other reason ?
# 12  
Old 03-29-2012
Quote:
Originally Posted by angie1234
Hi,

Can you please explain what is being done in the above three codes.
Thanks
Nr. 1 uses a double quote as the field separator and then deletes any non-digit from the start of field 9 and then prints field 9

Nr.2 uses any number of spaces as field separator and then prints the fifth column, which is the number with a $-sign

Nr.3 I figured you would probably be using a loop tot read the file line by line in your shell script, so I showed an alternative loop that would do the same without external programs and would then therefore be the fastest solution.
This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 03-29-2012
Quote:
Does piping it into one single expression made the difference ? Or is there any other reason ?
No. Just tried it with two lines using "tr" and it still works.

The "sed" is awkward because $ is special to "sed".
You can get it to work if you have enough backslashes.
Code:
a=`echo "${a}" | sed -e "s/\\\\$/ /g"`



Ps. Somebody posted a solution earlier which didn't have enough backslashes.

Last edited by methyl; 03-29-2012 at 12:00 PM..
# 14  
Old 03-29-2012
That's because of both the double quotes and those pesky backticks (I don't understand why anyone would still want to use them, unless they are forced to by their ancient shell).
Code:
a=$(echo "$a" | sed -e 's/\$//g')

Code:
a=$(echo "$a" | sed -e "s/\\$//g")

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

Code:
a=`echo "$a" | sed -e "s/\\\\$//g"`


Last edited by Scrutinizer; 03-29-2012 at 01:58 PM..
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