Home > Software engineering >  How to convert in specific column in csv in shell script linux
How to convert in specific column in csv in shell script linux

Time:10-06

i have a file output.csv like this :

No | Timestamp | Table | agg | result | percentage | status | year | month | day
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|287.472.321|-4.83%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|498.512.903|-4.05%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|19.089.222|-19.4%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|29.014.921|-2.47%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|148.770.415|-0.14%|Normal|2022|202209|20220930

all i wanna do is converting "." to blankspace in 5th column without affect on 6th column like this :

2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|287472321|-4.83%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|498512903|-4.05%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|19089222|-19.4%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|29014921|-2.47%|Normal|2022|202209|20220930
2022-09-30 00:00:00|2022-09-30 00:00:00|Source|count|148770415|-0.14%|Normal|2022|202209|20220930

i've tried using cat output.csv | grep -v -i "No " | sed 's/\.//g' > output3.csv But it will convert all columns that have "." .

So how to converting "." to blankspace in 5th column without affect on 6th column with still csv format output?

Thank you for the help!

CodePudding user response:

It is easier to do this in awk:

awk 'BEGIN{ FS=OFS="|" } { gsub("[.]","",$5) } 1' file

But if you insist on using sed:

sed '
    :a
    s/^\(\([^|]*|\)\{4\}[^|]*\)\./\1/
    ta
' file

CodePudding user response:

This might work for you (GNU sed and shell):

sed 'y/|/\n/;s/.*/echo "&"|sed "5s#\\.##g"/e;y/\n/|/' file

In essence, turn each line into a file and run a second invocation of sed where each address represents a column in the original line.

Translate |'s to newlines.

Write a mini sed invocation each time the pattern space is presented, that removes all .'s from the 5th line (each line in the invocation represents a column in the original line).

Translate newlines back to |'s.

  • Related