Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/Unix bash basic script awk/sed

Tags:

linux

csv

sed

awk

I'm working on bash script.

var=$(ls -t1 | head -n1);
cat $var | sed 's/"//g' > latest.csv
cat latest.csv | sed -e 's/^\|$/"/g' -e 's/,/","/g' > from_epos.csv
echo "LATEST: $var";

Here's the whole script, it's meant to delete all quotation mark from current file and add new one, between each field.

INPUT:

"sku","item","price","qty"
5135,"ITEM1",1.79,5
5338,"ITEM2",1.39,5
5318,"ITEM3",1.09,5
5235,"ITEM4",1.09,5
9706,"ITEM5",1.99,5

OUTPUT:

"sku","item","price","qty"
"5135","ITEM1","1.79","5
"
"5338","ITEM2","1.39","5
"
"5318","ITEM3","1.09","5
"
"5235","ITEM4","1.09","5
"
"9706","ITEM5","1.09","5
"

My ideal output is:

"sku","item","price","qty"
"5135","ITEM1","1.79","5"
"5338","ITEM2","1.39","5"
"5318","ITEM3","1.09","5"
"5235","ITEM4","1.09","5"
"9706","ITEM5","1.99","5"

It seems like it's entering random character between line in current output like " and quotation mark is between CR and LF.

What's the problem and how to get it to my ideal vision?

Thanks,

Adam

like image 235
Adam Lesniak Avatar asked Nov 21 '25 10:11

Adam Lesniak


2 Answers

awk 'BEGIN{FS=OFS=","}{gsub(/\"/,"");gsub(/[^,]+/,"\"&\"")}1' input
like image 63
jaypal singh Avatar answered Nov 22 '25 22:11

jaypal singh


Solution using sed:

sed -e 's/"//g; s/,/","/g; s/^/"/; s/$/"/'

Long-piped-commented version:

sed -e 's/"//g' | # removes all quotations
sed -e 's/,/","/g' | # changes all colons to ","
sed -e 's/^/"/; s/$/"/' # puts quotations in the start and end of each line
like image 42
Rudy Matela Avatar answered Nov 23 '25 00:11

Rudy Matela