LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2014, 06:38 AM   #1
rajakt
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Rep: Reputation: 0
Script for nested delimiter


Hi,

I have file having data as :

"aaa","aaa,vvvv,llll,kkk","bbbbb"
"g","pppp,kk,gg","caa,a,d,e,f","jjj,mmm,lll"

want the outout :
aaa|aaa,vvvv,llll,kkk|bbbbb
g|pppp,kk,gg|caa,a,d,e,f|jjj,mmm,lll

Can anyone provide me directions? I used cut -d, simple awk ,simple sed, but could not get thru in Linux.
 
Old 07-08-2014, 11:46 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
First one is a CSV file.
Code:
sudo apt-get install csvtool

csvtool -u '|' cat filename
 
1 members found this post helpful.
Old 07-08-2014, 12:39 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This transformation may be done in two steps.
1) Replace every instance of "," with |.
2) Replace every instance of " with null.

With this InFile ...
Code:
"aaa","aaa,vvvv,llll,kkk","bbbbb"
"g","pppp,kk,gg","caa,a,d,e,f","jjj,mmm,lll"
...this sed ...
Code:
sed 's/","/|/g; s/"//g' $InFile >$OutFile
... produced this OutFile ...
Code:
aaa|aaa,vvvv,llll,kkk|bbbbb
g|pppp,kk,gg|caa,a,d,e,f|jjj,mmm,lll
Daniel B. Martin
 
1 members found this post helpful.
Old 07-09-2014, 11:33 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
The sed is probably clearer / cleaner:
Code:
awk '{$1=$1}gsub(/"/,"")' FPAT='"[^"]*"' OFS="|" file
Or in ruby:
Code:
ruby -ne 'puts $_.scan(/"([^"]*)",?/).join("|")' file
 
1 members found this post helpful.
Old 07-09-2014, 02:29 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
For those who prefer an awk solution ...

This transformation may be done in two steps.
1) Replace every instance of "," with |.
2) Replace every instance of " with null.

With this InFile ...
Code:
"aaa","aaa,vvvv,llll,kkk","bbbbb"
"g","pppp,kk,gg","caa,a,d,e,f","jjj,mmm,lll"
...this awk ...
Code:
awk 'gsub(/","/,"|") gsub(/"/,"")' $InFile >$OutFile
... produced this OutFile ...
Code:
aaa|aaa,vvvv,llll,kkk|bbbbb
g|pppp,kk,gg|caa,a,d,e,f|jjj,mmm,lll
Daniel B. Martin
 
1 members found this post helpful.
Old 07-10-2014, 04:04 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
The problem with most sed/awk solutions is that CSV files are a bit complicated. Quotes are allowed in values, and also newlines. This example doesn't have any, but maybe it will sometimes later?

For example we have a value which is:

Code:
Hello
"world"
It's encoded as

Code:
"Hello
""world"""
This is tricky to parse. I think it's better to use a proper parser for CSV.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How use CUT -d 'delimiter' is delimiter is a TAB? frenchn00b Programming 12 11-06-2013 03:17 AM
[SOLVED] bash script - awk split field on specific delimiter ignore spaces. oly_r Programming 2 07-24-2013 09:49 AM
[SOLVED] Script to check files in nested directories in Linux zaayu87 Linux - Newbie 6 12-19-2012 11:41 AM
nested loop-bash script- issue on logic yathin Linux - Newbie 6 05-31-2010 06:30 AM
Nested Bash Script downbound010 Programming 1 12-10-2005 02:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration