[awk] add column between range pattern

Hi all,

I have table like this

A1
1      2
1      3
2      4 
A2
3      1
1      2
1      1
A3
1      1
2      0
3      2
.....
An

And i want to add column to my table and the table will become like this :

A1
1     2    A1
1     3    A1
2     4    A1
A2
3     1    A2
1     2    A2
1     1    A2
A3
1     1    A3
2     0    A3
3     2    A3
.....
An

Can someone kindly suggest what should i do with AWK to achieve this..

Hello,

Following may help you.

awk '/^A/ {f=$0} !/^A/ {$(NF+1)=f} 1' OFS="\t" check_add_col_check12113

Output will be as follows.

A1
1       2       A1
1       3       A1
2       4       A1
A2
3       1       A2
1       2       A2
1       1       A2
A3
1       1       A3
2       0       A3
3       2       A3
 

NOTE: Where check_add_col_check12113 is the input filename.

Thanks,
R. Singh

Try something like this

$ awk 'NF==1{x=$1;print;next}{print $0,x}' OFS='\t' file
A1
1      2	A1
1      3	A1
2      4 	A1
A2
3      1	A2
1      2	A2
1      1	A2
A3
1      1	A3
2      0	A3
3      2	A3
An
1 Like

Thanks Akshay, work perfectly like i want..