Comma separate issue in UNIX

In awk the field seprator is not working properly, I am trying to cut the fields from the file based on the delimiter example comma (,)

awk -F, "{print {$1 FS $3 FS $5 FS FS $2}}" Sample.csv

But i am not getting desired output can anyone help me how to check real ascii comma there in my sample.csv file if not what to do ?

Could you give us a bit of your sample.csv so we can see...
What OS and shell are you using?

Posted by rspwilliam:

Hello rspwilliam,

You can use ' character in place of " and try as follows.

awk -F, '{print {$1 FS $3 FS $5 FS FS $2}}' Sample.csv

Thanks,
R. Singh

fields=$(sed -r -e 's/-1/ /g' -e 's/,/ FS /g' \
  -e 's/([0-9]+)/\$\1/g' control_file.txt)

The first command converts control_file.txt into a suitable awk command like below,
control file contain the index need to be cut from the sample.csv

$1 FS $3 FS $5 FS FS $8 FS FS $4

Then passing to awk

awk -F, "{print ${fields}}" $1

Sample.csv

BP ID,Prepaid Account No,CurrentMonetary balance ,charge Plan names ,Provider contract id,Contract Item ID,Start Date,End Date
1100001538,001000002506,251,[B2] R2 LTE CHARGE PLAN ,00000000000000000141,[B2] R2 LTE CHARGE PLAN _00155D10E20D1ED39A8E146EA7169A2E00155D10E20D1ED398FD63624498DB4A,16-Oct-12,18-O*ct-12 
1100003404,001000004029,45.22,B0.3 ECS_CHARGE_PLAN DROP1 V3,00000000000000009349,B0.3 ECS DROP2 V0.2_00155D10E20D1ED39A8E146EA7169A2E00155D10E20D1ED398FD63624498DA2E,16-Nov-13,*23-Nov-13

---------- Post updated at 08:10 AM ---------- Previous update was at 08:02 AM ----------

Hello rspwilliam,

Kindly try the following it should help.

awk -F, '{print $1 FS $3 FS $5 FS FS $2}'  Input_file

Thanks,
R. Singh

n12:/home/vbe/wks/test/awk $ awk -F, '{print $1 FS $3 FS $5 FS FS $2}' 0008.sample
BP ID,CurrentMonetary balance ,Provider contract id,,Prepaid Account No
1100001538,251,00000000000000000141,,001000002506
1100003404,45.22,00000000000000009349,,001000004029

??? Seems to work...

Hi Singh,

The code should work but in for my csv its not working , But when i create sample dummy file its work perfect , some ASCII issue required help on that

So its more to do with the content of you csv file...
Have you tried to extract I dont know 20 first lines? and see if you already have issues?
Where does de csv file come from?

Posted by rspwilliam:

Hello rspwilliam,
Could you please make sure that file is not having Windows carriage return charcters etc. you can use tr -d "\r" command if there are those characters present in your input file and then use the awk command to get the expected output.

Thanks,
R. Singh

If you need to pass values from shell to awk variables, this is absolutely not the right method. You should instead pass values to an awk script this way:
awk -F, -vFields="$fields" '{ print Fields...}
By the way you can shorten awk script by specifying the OutPut Field Separator to ",". So this will give finally:
awk -F, -vFields="$fields" -vOFS="," '{print Fields, "sample", ...}' files...