Marco
Marco

Reputation: 10823

Sed and Regular Expression

I've this issue: I've a CSV file that contains some data in this format:

100,30,1.704,,7
101,,suka,,5

and I should import this CSV inside mysql table

As you can seen, this csv has empty fields, denoted by double commas ',,' . I want using sed and regular expression, define pattern ,, and if it's matched substituing it with number 58 inside my csv file . Anyone can help me, I don't knwo regex very well.

Upvotes: 1

Views: 215

Answers (4)

Vijay
Vijay

Reputation: 67221

In Vi editor ,go to command mode and type the below:

:%s/,,/,58,/g

Upvotes: 0

potong
potong

Reputation: 58420

This might work for you:

sed ':a;s/,,/,58,/g;ta' file

Upvotes: 3

mccbala
mccbala

Reputation: 31

You may try using notepad++ if you want regex options. Open Search and replace set the search mode as "Regular Expressions" replace ,, with ,58. I just tried it and it works... ,,,,,,,, to ,58,58,58,58

Upvotes: -1

umläute
umläute

Reputation: 31284

something like this should do the trick:

sed -e 's/,,/,58,/g'

which will search ('s') or ',,' and replace it by ',58,' for all occurences ('g'; as opposed to only the first occurence))

Upvotes: 1

Related Questions