Here is what am trying to do: My data is in sets of rows within same file (the variables vaa vbb are looping over it and am able to control it as required)
What i need is to add an extra column before the data is written to file which is basically a counter that iterates with each set of data.

Eg: Set1 of data row 5-8 , Set2 of data row 14-29, etc

Required output:

1,row5 data
1,row6 data
1,row7 data
1,row8 data
2,row14 data
2,row15 data
2,row29 data

====== Code below ======

awk -v vaa=$varAA -v vbb=$varBB -v vcc=$varC 'NR>=vaa&&NR<=vbb' $I >> part_${I%.*}.csv

I am writing the file to a csv file. I am able to handle the sets of row and counter variable. But unable to formulate the piece of code which can add the extra column feeding data using the variable $varC (which has the incrementing counter)

I have browsed through several forums and the usage/examples are either simply for printing or just adding a column in existing dataset.

I am new to bash coding so unable to understand how to accomplish this. All help is appreciated.


Edit: output_A.csv contains data output_A.txt contains the info regarding what region to what region is the sets of data. (some arithmetic is required which is task specific and i have taken care of) eg of txt file data: 100 200 xyz Here is the complete code for reference:

for I in 'output_A.csv';

while read line

varC=$(( varC + 1 ))

varB=$(echo "$line" | cut -d',' -f2- | rev | cut -d',' -f2- | rev)

varAA=$(echo "$varA * 100" | bc -l)
varBB=$(echo "$varB * 100" | bc -l)

#echo -e $varA ' \t' $varB ' \t' $line

awk -v vaa=$varAA -v vbb=$varBB -v vcc=$varC 'NR>=vaa&&NR<=vbb' $I >> part_${I%.*}.csv

done < ${I%.*}.txt

  • 1
    How should one determine when the first set of data has been exhausted and the counter should be increased? Please post a sample of the input file.
    – kos
    Jun 22, 2015 at 6:26
  • Where in your example are vaa, vbb, vcc ?
    – Costas
    Jun 22, 2015 at 6:44
  • just added the entire code in edits - apologies for missing it out in the first place Jun 22, 2015 at 6:48
  • It would be much better if you supply with samples of data files and preferrable output format
    – Costas
    Jun 22, 2015 at 6:54
  • @Costas my doubt is more syntactical in nature. I can figure out the task specific solution. The examples I see are something like: awk '{print "$variable name",$0}' I understand $0 is all columns of the row - How do I relate that to usage of NR controls which in my code controls the range of rows. Jun 22, 2015 at 6:57

2 Answers 2


As I understand it you need to change your awk command from:



'NR>=vaa&&NR<=vbb { print vcc "," $0 }'

According to format files explanation

awk -F',' 'FNR==NR{
               next }
           { print portion[FNR], $0 }
          ' output_A.txt output_A.csv >> output_A.result
  • thanks this gives me an idea to improve some parts but my issue is with handling the incremental counter varC which increments with each set of rows. Jun 22, 2015 at 7:34

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.