sed awk to remove the , in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed awk to remove the , in a string
# 1  
Old 03-21-2013
sed awk to remove the , in a string

Dear All,

Can anyone help to remove the , bewteen "" in a string by using sed or awk?

e.g.

input : 1,4,5,"abcdef","we,are,here",4,"help hep"
output:1,4,5,"abcdef","wearehere",4,"help hep"

Thanks,
Mimi
# 2  
Old 03-21-2013
Using awk:

Code:
awk '{for(i=2;i<=NF;i=i+2)gsub(",","",$i)}1' FS='"' OFS='"' file

Guru
# 3  
Old 03-21-2013
It works!! Thanks
# 4  
Old 03-21-2013
Using sed:

Code:
$ cat file
1,4,5,"abcdef","we,are,here",4,"help hep"

$ sed -r ':x s/("[^",]+),([^"]+")/\1\2/; tx' file
1,4,5,"abcdef","wearehere",4,"help hep"

$ sed ':x s_\("[^",]\+\),\([^"]\+"\)_\1\2_; tx' file
1,4,5,"abcdef","wearehere",4,"help hep"


Last edited by hanson44; 03-21-2013 at 03:47 AM..
# 5  
Old 03-21-2013
Quote:
Originally Posted by hanson44
Using sed:

Code:
$ cat file
1,4,5,"abcdef","we,are,here",4,"help hep"

$ sed -r ':x s/("[^",]+),([^"]+")/\1\2/; tx' file
1,4,5,"abcdef","wearehere",4,"help hep"

$ sed ':x s_\("[^",]\+\),\([^"]\+"\)_\1\2_; tx' file
1,4,5,"abcdef","wearehere",4,"help hep"

Code:
$ echo '1,4,5,"abcdef","we,are,here",4,"help hep"' | sed -e :x -e 's/"\([^",]\{1,\}\),/"\1/g; tx'
1,4,5,"abcdef","wearehere",4,"help hep"

# 6  
Old 03-21-2013
Yes, that's a better solution, a simpler solution. Thanks.

And of course you don't need the two -e expressions, they can be combined into one script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

2. Shell Programming and Scripting

sed remove everything between two string

Hi all, I have this input: "203324780",,"89321213261247090146","VfdsD150","0D","fd3221","V0343","aaa","Direkt","fsa","2015.02.27","39833,54454,21214",,,"fd","NORMAL","D","10fd","1243 Flotta","HiĂĄnytalan","2013.02.25",,"2013.02.25","2013.02.24","2013.02.28",,"SajĂĄt... (4 Replies)
Discussion started by: snayper
4 Replies

3. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Sed is doing my head in! How do you remove the first character of a string?

Hello! Please bare with me, I'm a total newbie to scripting. Here's the sudo code of what I'm trying to do: Get file name Does file exist? If true get length of file name get network id (this will be the last 3 numbers of the file name) loop x 2 If... (1 Reply)
Discussion started by: KatieV
1 Replies

6. Shell Programming and Scripting

use sed to remove year from string?

Hi guys, I have been trying to play with sed to accomplish this but I just can't quite get it right. I need to be able to remove the year from a string held in a variable in my bash script. The string may have multiple words but always ends with a year such as (2009) for example: ... (2 Replies)
Discussion started by: tret
2 Replies

7. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

8. Shell Programming and Scripting

A problem for sed? Remove parts of a string

Hi, My knowledge about sed is limited but I have a problem that I think can be solved with sed. I have a variable in a shell script that stores a lot of path/filenames and the delimitter between them is a space (they all exist on the same line). One part of the filename is the file creation... (4 Replies)
Discussion started by: pcrs
4 Replies

9. Shell Programming and Scripting

How to remove all matches in a string with sed

if I have "abxcdxefx" and want to remove the x's with sed, how can I do this? Thanks. WHOOPS: Just remembered: echo "abxcdxefx" | sed s/x//g Thanks for reading, though. (0 Replies)
Discussion started by: lumix
0 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question