Awk: Remove comma at the end of the string

Hi

i had String like

UID: ABC345QWE678GFK345SA90, LENGTH 32

when I used awk ' FS[" "], {print $1}' prints

ABC345QWE678GFK345SA90,

how can i getrid of that coma at the end of the string.

Thanks in advance..

You awk example does not seem to work. Do you mean something like this:

$> echo "UID: ABC345QWE678GFK345SA90, LENGTH 32"|awk -F'[, ]' '{print $2}'
ABC345QWE678GFK345SA90

Thanks for your reply.
I am using ksh.When i tried to use above awk command
its prints nothing blank screen appears.

Thanks.

Did you run Scrutinizer's example exactly like he has posted ? i.e. did you put the echo <string> and the "|" on the left of the awk command ?

The example works for me in ksh. (May be better to show us what you tried, so that we are on the same page.)

tyler_durden

I had a similar problem before. I used the replace function but you just replace it with nothing so the , gets removed.

code
put "" instead of '' and try again and do't forget there is a space after the ,

gawk -F"[, ]" '{print $2}'  file.txt

BR

Try:

echo "UID:  ABC345QWE678GFK345SA90, LENGTH 32"|awk -F'[, \t]*' '{print $2}'

instead..

Why would you use awk to operate on a string? The shell can manipulate strings without using an external command.

var=ABC345QWE678GFK345SA90,
newvar=${var%,}
printf "%s\n" "$newvar"

Agreed but the OP was using awk anyway to transform

UID:  ABC345QWE678GFK345SA90, LENGTH 32

into

ABC345QWE678GFK345SA90, 

So you would need a bit more than that to eradicate awk, like:

ts="UID:               ABC345QWE678GFK345SA90, LENGTH 32"
ts=${ts#*:}
echo "${ts%,*}"

We can change the expansion order:

ts="UID:               ABC345QWE678GFK345SA90, LENGTH 32"
ts="${ts%,*}"
echo ${ts#*:}

or using awk

echo "UID:  ABC345QWE678GFK345SA90, LENGTH 32"|awk '{sub(",","");print $2}'

or sed

echo "UID:  ABC345QWE678GFK345SA90, LENGTH 32" | sed 's/.* \(.*\),.*/\1/'

Thanks for you replies.

I had an executable file name " uid.out" that generates following ouput when i call it from the shell script.
"Home" is the string that i passed as input to the string.

Using Prod Key
Trying to Encrypt: home
UID: 8C42A3DA2A9BFB9D8E89E4FA9C9FE161, length: 32

Now i want to extract the 8C42A3DA2A9BFB9D8E89E4FA9C9FE161 into variable.
what i did was

uid.out "Home" |grep UID >> temp.txt
=$( awk '{FS=" "} {print $2}' temp.txt)
echo "${Ecode}
produces the ouptut 8C42A3DA2A9BFB9D8E89E4FA9C9FE161,
in which I am not able to seperate Coma(,) at the end string.

What am i doing wrong?

thanks in advance.

Reddy, have you tried any of the solutions? Leaving optimizations of your code aside, there are close to a gazillion solutions to your problem in this thread. If you are having trouble choosing then just pick one.

You are not paying close attention to the scripts posted here.

Try one of these:

uid.out "Home" | awk -F '[ ,]' '/UID/ {print $2}'

or

uid.out "Home" | awk -F "[ ,]" '/UID/ {print $2}'

or

uid.out "Home" | awk 'BEGIN{FS="[ ,]"} /UID/ {print $2}'

or

uid.out "Home" | perl -lne '/^UID: (\w+),/ && print $1'

tyler_durden

uid.out |
{
 read;read             ## discard first two lines
 IFS=' ,' read a b c   ## what you want is stored in $b
 printf "%s\n" "$b"
}

Thank you Johnson.
Your code worked like charm.

---------- Post updated 11-09-09 at 02:40 PM ---------- Previous update was 11-08-09 at 09:12 PM ----------

Hi Dan
Can you please explain me what is happenning in the sed command
sed 's/.* \(.*\),.*/\1/' temp.txt).It worked for me bit i didn't understand.