awk Help: Horizontal to Vertical print with pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Help: Horizontal to Vertical print with pattern match
# 1  
Old 01-15-2013
awk Help: Horizontal to Vertical print with pattern match

Hi AWK Experts,

Following is the data :


Code:
BRH113 DD AA HH CA DD DD AA HH BRH091 A4 A6 AH H7 67 HH J8 9J BRH0991 AA D8 C23 V5 H7 BR2 BRH991 AA HH GG5 BT0 JJ0


I want the output to be alligned with the pattern matching "BRH" inthe line.


The output should be look like:
A] Output:

Code:
BRH113	DD	AA	HH	CA	DD DD AA HH 
BRH091	A4	A6	AH	H7	67 HH J8 9J 
BRH0991	AA	D8	C23	V5	H7 BR2 
BRH991 	AA 	HH	GG5	BT0	JJ0


B] Output:
Code:
BRH113	BRH091	BRH0991	BRH991
DD	AA	AA	AA
AA	A6	D8	HH
HH	AH	C23	CG5
CA	H7	V5	BTO
DD	67	H7	JJO
DD	HH	BR2
AA	J8
HH	9J

Can we do it with awk, or other unix tool,


Thanks a lot,
Reveri.
# 2  
Old 01-15-2013
A] Output:
Code:
awk '{for(i=1;i<=NF;i++) ($i~/^BRH.*/)?$i=RS $i"\t":$i=$i"\t";}1' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-15-2013
A output:

Code:
sed 's/ BRH/\
BRH/g' infile

b output:

Code:
awk '{for(i=1;i<=NF;i++)
   if ($i ~ "^BRH") {
      key=$i
      keys[++k]=key
      l=1
   } else { m=l>m?l:m;v[key,l++]=$i }
}
END {
    OFS="\t"
    $0=""
    for(j=1;j<=k;j++) $j=keys[j]
    print
    for(i=1;i<=m;i++) {
        $0=""
        for(j=1;j<=k;j++) $j=v[keys[j],i];
        print;
    }
}' infile


Last edited by Chubler_XL; 01-15-2013 at 10:25 PM.. Reason: Update to keep column order correct
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 01-16-2013
try also:
Code:
awk '$1=$1 {print RS $0}' RS="BRH" OFS="\t" infile

and
Code:
awk '{
  for (i=1; i<=NF; i++) {
    if ($i ~ /^BRH/) {c++ ; d=1}
    if (d>m) m=d;
    col[c,d++]=$i "\t";
  }
  for (i=1; i<=m; i++) {
    for (j=1; j<=c; j++) printf col[j,i];
    print "";
  }
}' infile

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 01-17-2013
These are great codes, specially like the awk's of all three , bipinajith ,Chubler_XL ,rdrtx1 . Still I am trying to understand . Thanks a lot ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print vertical to horizontal

Hi Masters, I need help to change my vertical data to horisontal input 2015-04-13|JS|741667 2015-04-13|JSJ|2272 2015-04-13|TMS|107099 2015-04-12|JMD|47945 2015-04-13|TM|760024 2015-04-13|JM|484508 2015-04-14|JMJ|318 2015-04-14|JSD|54436 2015-04-13|JM|15410 Output... (2 Replies)
Discussion started by: radius
2 Replies

2. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

Script awk vertical to horizontal with some condition

hi all.. i have problem, right now i want to processing some data with input like this BIMAB ACF-0168 QTS-0465 QUA 2013-08-17 14:16:09.34 ** ALAR ORX -004 NDORIDUNGGA (21943) 7745 ABOVE DEFINED 02 00 01 00 00 00 01 00 00 01 03... (12 Replies)
Discussion started by: buncit8
12 Replies

4. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

5. Shell Programming and Scripting

awk in horizontal and vertical math

Based on input ail,UTT,id1_0,COMBO,21,24,21,19,85 al,UTHAST,id1_0,COMBO,342,390,361,361,1454 and awk code as awk -F, '{ K=0; for(i=NF; i>=(NF-4); i--) { K=K+$i; J=J+$i;} { print K } } END { for ( l in J ) printf("%s ",J); }' I'm trying to add columns and lines in single line. line... (6 Replies)
Discussion started by: busyboy
6 Replies

6. Shell Programming and Scripting

awk - horizontal and vertical text extraction

Hi, I need some help in getting extracting the specific horizontal and vertical texts in a single line. I am trying to extract few of the parameters from a config file. Your help is appreciated. Desired Output ---------------- Pool members members ... (4 Replies)
Discussion started by: pratheeshp
4 Replies

7. Shell Programming and Scripting

Print strings that match pattern with awk

I have a file with many lines which contain strings like .. etc. But with no rule regarding field separators or anything else. I want to print ONLY THE STRING from each line , not the entire line !!! For example from the lines : Flow on service executed with success in . Performances... (5 Replies)
Discussion started by: black_fender
5 Replies

8. UNIX for Dummies Questions & Answers

Horizontal to vertical

Hi, Silly question, if I have an excel file that looks something like this: ................. Subject 1 Subject 2 Subject 3 Subject 4 Fever..............13...........9.............23..........14 Headache.........2............12...........18..........23... (3 Replies)
Discussion started by: Xterra
3 Replies

9. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies

10. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies
Login or Register to Ask a Question