[awk] add column between range pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] add column between range pattern
# 1  
Old 04-11-2014
[awk] add column between range pattern

Hi all,

I have table like this

HTML Code:
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 :

HTML Code:
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..
# 2  
Old 04-11-2014
Hello,

Following may help you.

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

Output will be as follows.

Code:
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

Last edited by RavinderSingh13; 04-11-2014 at 01:47 AM.. Reason: Added tab as Output seprator
# 3  
Old 04-11-2014
Try something like this

Code:
$ 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

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 04-11-2014
Quote:
Originally Posted by Akshay Hegde
Try something like this

Code:
$ 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

Thanks Akshay, work perfectly like i want..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. UNIX for Beginners Questions & Answers

How to sum value of a column by range defined in another file awk?

I have two files, file1.table is the count table, and the other is the range condition file2.range. file1.table chr start end count N1 0 48 1 N1 48 181 2 N1 181 193 0 N1 193 326 2 N1 326 457 0 N1 457 471 1 N1 471 590 2 N1 590 604 1 N1 604 752 1 N1 752 875 1 file2.range... (12 Replies)
Discussion started by: yifangt
12 Replies

3. Shell Programming and Scripting

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

4. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

5. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

6. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

7. Shell Programming and Scripting

AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread. I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well. example: PACKET DATA PROTOCOL CONTEXT DATA APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID 10 ... (6 Replies)
Discussion started by: panapty
6 Replies

8. UNIX for Dummies Questions & Answers

Search Pattern and add column

Hi, I have two files. file1 contents: aaa bbb ccc ddd eee fff ggg ddd www eee ggg dde qqq zzz hhh ddd file2 contents: mmm mmm mmm mmm Now I want to add file2 contents to end of lines in file1 where a line contains pattern "ddd" and it should look like this: file3 contents: aaa... (3 Replies)
Discussion started by: harjitsingh
3 Replies

9. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

10. Shell Programming and Scripting

give column range in awk

hi all, I generally give an awk command to print multiple columns like this: awk -F~ '{OFS=",";print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' test.txt can't we give a range like : awk -F~ '{OFS=",";print $1-$13}' ( I know this will subtract column 13 from 1) or awk -F~... (1 Reply)
Discussion started by: sumeet
1 Replies
Login or Register to Ask a Question