re-Substitution Sed (or Perl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting re-Substitution Sed (or Perl)
# 1  
Old 08-03-2011
re-Substitution Sed (or Perl)

I have a large text csv file that I'm working with. It will look something like this:
Code:
D,",E",C
O,"F,",I
O,gh,R

The second column always has a two digit random code (can be numbers, letters or any characters). When one of the characters happens to be a comma, the string is quoted. I want to find a way (within the context of a larger shell script i'm writing) to change that around. Ideally I would get rid of the quotes and change the comma to some other character - lets say a double colon ::.

So the output would look like:
Code:
D,::E,C
O,F::,I
O,gh,R

I can do something like this:
Code:
sed -i 's/"*."/::/g' myfile

(or the equivlant in perl - i find sed operates faster on big files).

but that obviously doesnt get me what i want. The question is i want to re-substitute in the other character do you have any thoughts on how to do that?

Thanks,

bg

Last edited by Franklin52; 08-06-2011 at 04:17 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-03-2011
When you are working with big and weird CSV file the best way (and I believe the only good way) is to use a good csv library. Text::CSV for perl from CPAN is an excellent choice.
# 3  
Old 08-04-2011
Use
Code:
sed -i -e  s/\"(.*)\,(.* )\"/\1::\2/g

I think this will work

Last edited by Franklin52; 08-06-2011 at 04:18 PM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 08-04-2011
Thanks for the reply.

When I run that I get the following error:
Code:
]$ sed -i -e s/\"(.*)\,(.* )\"/\1::\2/g myfile.txt
bash: syntax error near unexpected token `('

However when I try it in perl:
Code:
$perl -pe 's/\"(.*)\,(.*)\"/$1::$2/g' myfile.txt

it works fine - so thanks!

any reason why the sed version doesn't work? just trying to learn more about scripting. the passing the matched string as a variable is a trick i wasn't familiar with and is great to know...

thanks,

bg

Last edited by Franklin52; 08-06-2011 at 04:18 PM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 08-04-2011
minor correction:
Code:
sed -e 's/"\(.*\),\(.*\)"/\1::\2/g' myfile

# 6  
Old 08-04-2011
Because 'sed' is different of 'perl' for brackets:

perl -> (...)
sed -> \(...\)

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk/perl substitution with multiple lines

OSX I have been grinding my teeth on a portion of code. I am building a bash script that edits a html email template. In the template, I have place holders for SED (or whatever program is appropriate) to use as anchors for find and replace, with user defined corresponding html code. The HTML code... (3 Replies)
Discussion started by: sudo
3 Replies

2. Shell Programming and Scripting

Perl substitution

Hi, I'm new to Perl, and I want to change a few columns in a file in order to insert them into a database. The input file looks like this: 00001,"01/1234567" ,"Tst2" 00002,"01/4545646" ,"Tst123456" 00003,"01/8979898" ,"" The output should look like this: 01-1234567,00001... (2 Replies)
Discussion started by: Subbeh
2 Replies

3. Shell Programming and Scripting

perl substitution

Hello all I have a strings like " Watch news 24x7 "."x-wars is glowing" " Watch news like 24 x 7"."x-mas will be celebrated" " Dimensions of box is 24x23x47 ". I have to remove the x(by) in between the number. If i just replace x, it will also remove all x's from text which i do not want.... (1 Reply)
Discussion started by: vasuarjula
1 Replies

4. Shell Programming and Scripting

substitution using perl

Hi All, I need the perl version of the below sed command: sed 's/abc.*/&.txt/g' <filename> Because I'm trying to do some replacement recursively using perl and the above replacement is replacing the abc* with "&.txt" exactly. Thanks, Arun (9 Replies)
Discussion started by: arun_maffy
9 Replies

5. Shell Programming and Scripting

sed substitution

Using sed I'm trying to replace 'string' with ']' while retaining case and ignoring words with 'string' in it along with additional characters like 'strings' and those which already contain the ] wrapper. I'm hoping to do it with sed and the right expression, if possible. Example: Apple... (2 Replies)
Discussion started by: tom.lee
2 Replies

6. Shell Programming and Scripting

Help with sed/substitution!

I have file.txt 1 4 7 9 3 I want to replace the tabs with a space, but my code doesn't work. cat file.txt | sed 's/"\t"/ /g' > t.txt But file is still the same. Numbers seperated by tabs instead of spaces. Help? (2 Replies)
Discussion started by: Bandit390
2 Replies

7. Shell Programming and Scripting

SED Substitution

Hi guys, Can u please help me to replace (-) with (/) in a file containing no of records using "sed " command in unix. thanks in advance. subhendu (5 Replies)
Discussion started by: subhendu81
5 Replies

8. Shell Programming and Scripting

Substitution using SED

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as... (2 Replies)
Discussion started by: shubhranshu
2 Replies

9. Shell Programming and Scripting

Perl Substitution

I have lines in a file that look like this: machine: machinea machine: machineb machine: randomwhatevermachine I want to replace the machine lines with: machine: machinec I tried perl -pi -e "s/#machine:\?*/machine: machinec/" filename But this ended up doing this: ... (2 Replies)
Discussion started by: Lindarella
2 Replies

10. Shell Programming and Scripting

Using varible/varible substitution in Perl/sed Search & Replace

Hi, I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl: for i in... (3 Replies)
Discussion started by: Breen
3 Replies
Login or Register to Ask a Question