Find the answer to your Linux question:
Results 1 to 4 of 4
I need to edit 4th field in the CSV file (i need to subtract 5 from whatever number is their in the fourth field, if the number is negative then ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2

    Need help creatig a bash script to edit CSV File

    I need to edit 4th field in the CSV file (i need to subtract 5 from whatever number is their in the fourth field, if the number is negative then replace it with 0)
    The separator is ,

    Please help

  2. #2
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2
    Sample for CSV i am trying to edit

    Online,New World,20080819,0,1200M05,045468004016,N,,42,94,,,, ,,N
    Online,New World,20080819,13,1200M055,045468004023,N,,42,94,, ,,,,N
    Online,New World,20080819,0,1200M06,045468004030,N,,42,94,,,, ,,N
    Online,New World,20080819,67,1200M065,045468004047,N,,42,94,, ,,,,N

    Thanks in advance

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Here's an example using awk. Let's assume the name of the file is cvs.txt

    Code:
    awk -F',' '{ nmbr=$4; nmbr=nmbr-5; if (nmbr < 0) nmbr=0; $4=nmbr; print $0 }' cvs.txt

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'BEGIN{ FS=","; OFS=","}{ $4-5<=0 ? $4=0 : $4=$4-5 }$1=$1' file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...