LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-12-2016, 09:04 PM   #1
bosong
LQ Newbie
 
Registered: Mar 2016
Posts: 16

Rep: Reputation: Disabled
replacing of , between " " in linux


input > 20000000,"xxxxxxxxxxxxx,xxxxxxxxxxx", 192.168.3.2
Ideal result >20000000,"xxxxxxxxxxxxxxxxxxxxxxxx", 192.168.3.2

How do I get rid of the comma between the quotes?
There are also lines without the comma between the quotes
Thanks in advance
 
Old 04-12-2016, 09:40 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
And your attempts so far ?. Why do you think they failed ?.
 
Old 04-12-2016, 09:41 PM   #3
bosong
LQ Newbie
 
Registered: Mar 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
I did try using grep to take out the lines with comma in quotes then replacing them, afterward combining the 2 files, however if i did that it would no longer be in order.
 
Old 04-12-2016, 09:53 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed would my tool of choice - but you need to be able to precisely define the string in regex, and use back-references.
For example:
- only one set of double-quotes per line ?
- any possibility of un-matched double-quotes ?
- any possibility of multiple commas within double-quotes ?
...

Nothing is ever as easy as it first looks. Especially when we don't have all the specifications and/or data.
 
Old 04-12-2016, 09:55 PM   #5
bosong
LQ Newbie
 
Registered: Mar 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
E.g
Exchange subsidary,Passed,00021423SNG,R-JAM-05-03,US (First Exchange),20000000,"JUDICIARY, STATE COURTS (STATE COURTS)",112.78.212.12/30,00052312SNG,R-JPODIU-023-07,US (First Exchange) ,20000000,"JUDICIARY, STATE COURTS (STATE COURTS)",112.78.224.213/30


There are also line like this (without comma between in the " ")
Exchange subsidary,Passed,00021423SNG,R-JAM-05-03,US (First Exchange),20000000,"xxxxxxxxxxxxx",112.78.212.12/30,00052312SNG,R-JPODIU-023-07,US (First Exchange) ,20000000,"xxxxxxxxxxxxxx",112.78.224.213/30

I need to remove the comma inside ,"JUDICIARY, STATE COURTS (STATE COURTS)" (both)
Thanks in advance
 
Old 04-12-2016, 10:41 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So now that you have shown actual data your problem is compounded as now you have many combinations and possibilities, so many in fact that it may not be possible and you may have to simply hand alter
the file.

I would say that with a good editor like vim you should be able to quickly find and change any such commas.
 
Old 04-12-2016, 10:42 PM   #7
bosong
LQ Newbie
 
Registered: Mar 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
Ok thanks for the advice
 
Old 04-12-2016, 11:35 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
This should work - caveats apply, but it should remove (totally) the first comma within each set of double-quotes. If some remain, just run it again. Keep a backup of the input file before you start.
Code:
sed -r 's/("[^"]+),([^"]+")/\1\2/g' input.file
 
Old 04-13-2016, 01:16 AM   #9
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
try this sed command below, as syg00 suggests backup the input file before doing anything.

Quote:
sed -e 's/\("[^"]*\)[,]\([^"]*"\)/\1\2/g' file.old > file.new

Last edited by JJJCR; 04-13-2016 at 01:16 AM. Reason: edit
 
Old 04-13-2016, 01:17 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
isn't this some sort of .json file?
wouldn't it be easier to use tools designed to deal with json data?

(not a rhethorical question; i am currently facing a similar problem with some database text file, i think it's json)
 
Old 04-13-2016, 05:41 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
what about using " as line separator and replace , only in every second line?
 
Old 04-13-2016, 11:30 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by pan64 View Post
what about using " as line separator and replace , only in every second line?
With " as field separator, and replace in every second field
Code:
awk 'BEGIN {OFS=FS="\""} {for (i=2; i<=NF; i+=2) sub(/,/,"",$i); print}' inputfile

Last edited by MadeInGermany; 04-13-2016 at 11:39 AM.
 
Old 04-13-2016, 12:11 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
awk -v 'RS="' -v 'ORS="' 'NR%2 == 0 { gsub(",", "") } 1' input
 
1 members found this post helpful.
  


Reply

Tags
coding, linux, software



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
[SOLVED] Stop bash from replacing "~" with "/home/username" when TAB is pressed dedec0 Linux - Software 3 06-04-2015 01:24 PM
[SOLVED] Replacing all chars "ё" at screen to "ö" Nordman Linux - Desktop 17 12-21-2010 04:56 PM
How to stop bash from replacing "~/" with "/home/username"? daihard Linux - Software 8 08-22-2010 02:32 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:37 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