I have a csv file that looks like this:

2017-06-01,3967485,Completed,local,in,0,13492,11746,2017-06-27 14:13:47+00,2017-06-27 14:20:16+00,389,ddb19040,mydomain.net,yourdomain.net,""
2017-07-01,3967488,Completed,local,in,0,13492,11746,2017-07-27 14:13:47+00,2017-06-27 14:20:16+00,389,ddb19040,mydomain.net,yourdomain.net,""

What I need to do is write a bash script that will loop through this file line by line, and grab the first field as one value, and then save the rest of the line without the first field (and comma) as a separate variable.

I have the following code so far:

 for aline in $(cat /tmp/test.csv); do                         
       echo $aline 

But i'm not sure how to parse each line. Any pointers would be appreciated.


Using the suggested answer produces the following output in new_file

   ,Completed,local,in,0,13492,11746,2017-06-27 14:13:47+00,2017-06-27 14:20:16+00,389,ddb19040,mydomain.net,yourdomain.net,""
   ,Completed,local,in,0,13492,11746,2017-07-27 14:13:47+00,2017-06-27 14:20:16+00,389,ddb19040,mydomain.net,yourdomain.net,""

I'd like it to include field2 ($b in the suggested answer) at the start of each line


How about using awk:

awk 'BEGIN{FS=",";OFS=","}{$1=""; print}' file_name
  • so do this after the echo ? – dot Feb 28 '18 at 22:04
  • @dot - no need for anything except this simple one line. Just replace file_name with your file, eg /tmp/test.csv and you're off to the races. If your input is huge, you'll also notice that this solution is much more computationally efficient than using bash (ie. it will finish faster) – user1404316 Feb 28 '18 at 22:09
  • @dot - did you get it working? – user1404316 Mar 1 '18 at 3:36

Try this :

while IFS=, read -r a b; do echo "$b" >> new_file; done < file
cat new_file


  • so that almost works except that new_file is missing field 2... and starts with a comma. will update my original question with the sample outupt so you can see – dot Feb 28 '18 at 21:55
  • POST edited, try again please – Gilles Quenot Feb 28 '18 at 21:59
$ IFS=,
$ while  read -r a b ;do onevalue=$a; restvalue=$b; echo $a ;done <CSV_FILE >FirstF
$ cat FirstF

put your processing code for the rest value by making use of $b variable inside loop block one val for each one loop


You can use sed

sed 's/[^,]*,//' csv.file

If you want to keep the first col in a file

sed -e 'h;s/,.*//w col1.file' -e 'x;s/[^,]*,//' csv.file

Or with gnu sed

sed -e 's/,/\n/;W col1.file' -e 's/[^\n]*\n//' csv.file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.