Awk: Remove comma at the end of the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Remove comma at the end of the string
# 8  
Old 11-08-2009
Quote:
Originally Posted by Reddy482
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.

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

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

# 9  
Old 11-08-2009
Agreed but the OP was using awk anyway to transform
Code:
UID:  ABC345QWE678GFK345SA90, LENGTH 32

into
Code:
ABC345QWE678GFK345SA90,

So you would need a bit more than that to eradicate awk, like:
Code:
ts="UID:               ABC345QWE678GFK345SA90, LENGTH 32"
ts=${ts#*:}
echo "${ts%,*}"

# 10  
Old 11-08-2009
Quote:
Originally Posted by Scrutinizer
Code:
ts="UID:               ABC345QWE678GFK345SA90, LENGTH 32"
ts=${ts#*:}
echo "${ts%,*}"

We can change the expansion order:
Code:
ts="UID:               ABC345QWE678GFK345SA90, LENGTH 32"
ts="${ts%,*}"
echo ${ts#*:}

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

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

# 11  
Old 11-08-2009
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.
# 12  
Old 11-08-2009
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.

Last edited by Scrutinizer; 11-08-2009 at 12:56 PM..
# 13  
Old 11-08-2009
Quote:
Originally Posted by Reddy482
...
What am i doing wrong?
...
You are not paying close attention to the scripts posted here.


Quote:
Originally Posted by Reddy482
...
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.
...
Try one of these:

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

or

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

or

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

or

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

tyler_durden
# 14  
Old 11-08-2009
Quote:
Originally Posted by Reddy482
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.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

2. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

3. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

4. Shell Programming and Scripting

Remove 3rd character from the end of a random-length string

Hi, I hope someone can share there scripting fu on my problem, I would like to delete the 3rd character from a random length of string starting from the end Example Output Hope you can help me.. Thanks in advance.. (3 Replies)
Discussion started by: jao_madn
3 Replies

5. Shell Programming and Scripting

Remove lines that match string at end of column

I have this: 301205 0000030000041.49000000.00 2011111815505 908 301205 0000020000029.10000000.00 2011111815505 962 301205 0000010000027.56000000.00 2011111815505 3083 312291 ... (2 Replies)
Discussion started by: herot
2 Replies

6. Shell Programming and Scripting

BASH: remove digits from end of string

Hi there, im sure this is really simple but i have some strings like this e1000g123001 e1000g0 nge11101 nge3and i want to create two variables ($DRIVER and $INSTANCE). the first one containing the alpha characters that make up the first part of the string, e.g. e1000g or nge and the... (9 Replies)
Discussion started by: rethink
9 Replies

7. Shell Programming and Scripting

Remove comma and next rows beginning from the end

Hello friends, I have a file which consists of many rows, I use a couple of commands to convert it so i can use in a database query for filtering. I need the first columns (msisdns) in a row, seperated with commas, 9855162267,4,5,2010-11-03 17:02:07.627 9594567938f,5,5,2010-11-02... (9 Replies)
Discussion started by: EAGL€
9 Replies

8. Shell Programming and Scripting

Search and remove digits (if exist) from end of the string

Hi Experts, Here is what I am trying to do. 1) say I have a file with below strings database1 database2 database3 data10gdb1 data10gdb2 databasewithoutdigit 2) I want to get the below output. (- if there is any digit at the end of the string, I need to remove it) (- Any... (3 Replies)
Discussion started by: shail_boy
3 Replies

9. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

10. Shell Programming and Scripting

Remove / at the end of a string if it exists

Hey guys, Can sed or another command be used to examine a path such as /home/test/blah/blahand check to see if the last character is a "/" and if so remove it So this is what should happen: /home/test/blah/blahnothing happens, this is ok /home/test/blah/blah/the "/" at the end is... (6 Replies)
Discussion started by: tret
6 Replies
Login or Register to Ask a Question