Get string between quotes separate by commas


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get string between quotes separate by commas
# 1  
Old 03-07-2012
Get string between quotes separate by commas

I'm a beginner with shell and tried to do this per hours and everytinhg gives different want i do.

So

I have a lot of file in *.csv ( a.csv, b.csv ...)
in each file csv , it has some fields separeted by commas.
-----
Code:
"joseph";"21","m";"groups";"j.j@gmail.com,j.j2@hotmail.com"
"albert";"22";"m";"groups2";"a.a@gmail.com"

-----

I want to get a list of the fourth field (groups)
Result:
---
Code:
groups
groups2

---

Last edited by Franklin52; 03-07-2012 at 03:57 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 03-07-2012
I think you mean separated by semi-colons. There is a comma mixed in there though.
The quick answer:
Code:
nawk -F";" '{gsub(/\"/,"",$4);print $4}' x.csv

But if you want to save the columns into variables for further processing, you could do something like this:

Code:
#!/bin/ksh

INFILE=x.csv

#  Read a semi-colon delimited file, setting variables. Quotes preserve spaces.
while IFS=";" read "name" "age" "sex" "group" "email"
do
  
  print "$group"
 
  # If your shell supports string operations, use this which strips the double-quotes:
  # print "${group//\"/}"

done < $INFILE

exit 0


Last edited by gary_w; 03-07-2012 at 11:54 AM.. Reason: Use nawk to strip double-quotes
# 3  
Old 03-07-2012
Code:
$ awk 'BEGIN{FS="\""}{print $8}' data
groups
groups2

# 4  
Old 03-07-2012
I realized my awk example above did not strip double-quotes. Updated the awk command to nawk using the gsub function to do so.
# 5  
Old 03-07-2012
yes, semi colons.
thanks, I will try both tomorrow.

But I have many csv files to read, awk works with a ' for'?
# 6  
Old 03-09-2012
Done!

...
for f in `cat $file`
do
nameGroups=`echo $f | awk -F';' '{ print $3 }'`
# 7  
Old 03-09-2012
Your solution is not stripping double-quotes, and looks like your $file contains a list of files. In that case, try this (as a rule I put constants in caps and typeset them to be readonly, a habit from my C days):
Code:
#!/bin/ksh
typeset -r FILE="listofcsvfiles.txt"
while read filename
do
  print "\n\n${filename}:"
   nawk -F";" '{gsub(/\"/,"",$4);print $4}'  $filename
done < $FILE

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove quotes and commas from field

In the attached file I am trying to remove all the "" and , (quotes and commas) from $2 and $3 and the "" (quotes) from $4. I tried the below as a start: awk -F"|" '{gsub(/\,/,X,$2)} 1' OFS="\t" enhancer.txt > comma.txt Thank you :). (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Shell script that should remove unnecessary commas between double quotes in CSV file

i have data as below 123,"paul phiri",paul@yahoo.com,"po.box 23, BT","Eco Bank,Blantyre,Malawi" i need an output to be 123,"paul phiri",paul@yahoo.com,"po.box 23 BT","Eco Bank Blantyre Malawi" (5 Replies)
Discussion started by: mathias23
5 Replies

3. Shell Programming and Scripting

How to match fields surrounded by double quotes with commas?

Hello to all, I'm trying to match only fields surrounded by double quotes that have one or more commas inside. The text is like this "one, t2o",334,"tst,982-0",881,"kmk 9-l","kkd, 115-001, jj-3",5 The matches should be "one, t2o" "tst,982-0" "kkd, 115-001, jj-3" I'm trying with... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

4. Shell Programming and Scripting

How to delete the commas in a .CSV file that are enclosed in a string with double quotes?

Okay, I would like to delete all the commas in a .CSV file (TEST.CSV) or at least substitute them with empty space, that are enclosed in double quote. Please see the sample file as below: column 1,column 2,column 3,column 4,column 5,column 6,column 7,column 8,column 9,column 10... (8 Replies)
Discussion started by: dhruuv369
8 Replies

5. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

6. Shell Programming and Scripting

Preserve commas inside double quotes (perl)

Hi, I have an input file like this $ cat infile hi,i,"am , sam", y hello ,good, morning abcd, " ef, gh " ,ij no, "good,morning", yes, "good , afternoon" from this file I have to split the fields on basis of comma"," however, I the data present inside double qoutes should be treated as... (3 Replies)
Discussion started by: sam05121988
3 Replies

7. UNIX for Dummies Questions & Answers

Delete only commas in a string in AWK

How can I delete just commands in a string. I tried x = gsub(/,/,"",$1); (7 Replies)
Discussion started by: codecaine
7 Replies

8. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

9. Shell Programming and Scripting

how to find the count of commas in a string excluding the ones in double quotes

Hi, my requirement is to find the count of commas in a string excluding the ones in double quotes. For example: If the input string is abc,xyz.com,lmhgdf,"abc, 401 street","tty,stt",45,23,45 The output should be 7 (7 Replies)
Discussion started by: amitshete
7 Replies

10. Shell Programming and Scripting

Help with removing additional commas in string

Hi Experts, I have below strings hello,hi,,,,,,start date age,code,,,,,61,season I am trying to format this string to hello,hi,start date age,code,61,season Can anyone please help me in achieving this? Kind Regards, RB (3 Replies)
Discussion started by: ramakanth_burra
3 Replies
Login or Register to Ask a Question