2

I have a large csv file about 25 million lines and 256 columns. CSV file's field separator is ~ character.

I want to replace first 2 columns data like that

From : data1~data2~data3..................................
To   : "data1"~"data2"~data3................................

I am using currently awk but like this way:

cat file | awk -F "~" '{print "\""$1"\"""\""$2"\"~"$3"~"$4"~"...................}'

So I am typing 256 columns in awk like $4"~"$5"~"$6"~".....$256"~"

Is there any other better way to code it?

2 Answers 2

2

No, you don't need specify all 256 fileds, do it as following to changes only first and second columns, then perform print with OFS='~' and avoid cating to awk while awk can read from file as well by itself alone.

awk -F'~' '{$1="\""$1"\""; $2="\""$2"\""; print}' OFS='~' infile

To have changes on two last fields.

awk -F'~' '{$(NF-1)="\""$(NF-1)"\""; $NF="\""$NF"\""; print}' OFS='~' infile
3
  • 1
    Thank you so much, I did not know this way Commented Oct 18, 2017 at 7:36
  • If I want to change last 2 columns, how can I do it ? Commented Oct 18, 2017 at 7:48
  • see my update, please Commented Oct 18, 2017 at 7:54
1

With sed one would typically write

sed 's/[^~]*/"&"/;s//"&"/2'

But because you mentioned a large file, so this one will probably be much faster than the solution above or the awk version:

sed 's/\(^[^~]*\)~\([^~]*\)/"\1"~"\2"/'

Or, maybe easier to read, with extended regular expressions:

sed -E 's/(^[^~]*)~([^~]*)/"\1"~"\2"/'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .