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.
Thanks.
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';
do
varC=0
while read line
do
varC=$(( varC + 1 ))
varA=${line%%,*}
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
done
vaa
,vbb
,vcc
? – Costas Jun 22 '15 at 06:44I 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.
– NishantNath Jun 22 '15 at 06:57The data in output_A.txt is of format: 11.2345,11.2565,xyz
The data in output_A.txt means 1123-1125 lines are 1 set. (hence the multiplication by 100 to obtain the row number in varAA & varBB
The desired output is: (set no. eg; 1),0.123,1.234,....(about 200 columns)
– NishantNath Jun 22 '15 at 07:08