How can i comma-delimited last field in line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i comma-delimited last field in line?
# 1  
Old 06-25-2014
How can i comma-delimited last field in line?

Awk gurus,

Greatly appreciate for any kind of assistance from the expert community

Input line:
Code:
abc,11.22.33.44,xyz,7-8-9-10
pqr,111.222.333.444,wxy,1-2-3
def,22.33.44.55,stu,7-8

used the gsub function below but it changes all of the "-" delimiter:
Code:
awk 'gsub("-",",")'

Desired output:
Code:
abc,11.22.33.44,xyz,7-8-9,10
pqr,111.222.333.444,wxy,1-2,3
def,22.33.44.55,stu,7,8

# 2  
Old 06-25-2014
Be careful - if there's no minus in a line, it wont print!
Try
Code:
awk '{sub(/-[0-9]*$/,",&", $NF); sub (/,-/,",", $NF)}1' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-25-2014
Thanks RudiC.. I added alphanumeric search/sub as well per below:

Code:
awk '{sub(/-[aA0-zZ9]*$/,",&", $NF) ; sub (/,-/,",", $NF)}1'

# 4  
Old 06-25-2014
Quote:
Originally Posted by ux4me
Thanks RudiC.. I added alphanumeric search/sub as well per below:

Code:
awk '{sub(/-[aA0-zZ9]*$/,",&", $NF) ; sub (/,-/,",", $NF)}1'

Almost. [aA0-zZ9] doesn't mean what you seem to think it means. Try:
Code:
awk '{sub(/-[a-z0-9A-Z]*$/,",&", $NF) ; sub (/,-/,",", $NF)}1'

or:
Code:
awk '{sub(/-[[:alnum:]]*$/,",&", $NF) ; sub (/,-/,",", $NF)}1'

# 5  
Old 06-25-2014
That bracket expression is not, hm, invalid, but it might not match what you intend it to: it will match "a", "A", ALL chars from "0" to "z" (which includes a, A, Z, 9, but also e.g. "=" and "[", and, maybe some locale chars), then "Z" and "9". Make it [0-9A-Za-z] to include exactly those ranges.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

3. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. UNIX for Dummies Questions & Answers

How to change a line of text to a comma delimited string?

Hi, Is there a one-liner that I can use to change a line of text into a comma delimited string? For example, convert user1 user2 user3 user4to user1,user2,user3,user4Currently using while read x, although got the extra comma at the end that I have to remove manually. Please... (5 Replies)
Discussion started by: newbie_01
5 Replies

5. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

6. Shell Programming and Scripting

How to extract field delimited by comma and store into an array?

hi, i have a variable which contains some file names separated by comma. example FNAME="abc.txt,def.txt,ghi.txt" i want to extract each filename and store it into an array and also count the number of files in the array. file=abc.txt file=def.txt file=ghi.txt i thought of using the... (8 Replies)
Discussion started by: Little
8 Replies

7. Shell Programming and Scripting

Need a script to convert comma delimited files to semi colon delimited

Hi All, I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv... (4 Replies)
Discussion started by: CarpKing
4 Replies

8. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question