Script changing row to column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script changing row to column
# 1  
Old 01-12-2017
Script changing row to column

Hi Gurus,

I have an I/P file which looks like


Code:
100 1
200 1
300 4
100 2
200 3
300 4
100 9
200 8
300 7

I would liek to get O/P as

Code:
100 200 300
 1     1    4
 2     3    4
 9     8    7

Is it possible

Thanks,
# 2  
Old 01-12-2017
Hello Indra2011,

Could you please try following and let me know if this helps you.
Code:
awk '{if(!($1 in A)){C[++o]=$1;};++A[$1];B[$1,A[$1]]=$NF;P=P>A[$1]?P:A[$1]} END{for(f=1;f<=o;f++){printf("%d\t",C[f])};print X;for(j=1;j<=P;j++){for(l=1;l<=o;l++){printf("%d\t",B[C[l],j])};print X}}'   Input_file

Output will be as follows.
Code:
100    200    300    
1    1    4    
2    3    4    
9    8    7

EDIT: Adding a non-one liner form of solution too successfully now.
Code:
awk '{
        if(!($1 in A)){
                        C[++o]=$1;
                      };
        ++A[$1];
        B[$1,A[$1]]=$NF;
        P=P>A[$1]?P:A[$1]
     }
    END{
        for(f=1;f<=o;f++){
                                printf("%d\t",C[f])
                         };
        print X;
        for(j=1;j<=P;j++){
                                for(l=1;l<=o;l++){
                                                        printf("%d\t",B[C[l],j])
                                                 };
                         print X
                         }
       }
    '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-12-2017 at 03:28 PM.. Reason: Added non-one liner form of solution too successfully now. EDIT2: As per Made In Germany's suggestion !A[$1] to if(!($1 in A)
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-12-2017
Another one you could try:
Code:
awk '
  {
    if($1 in A) {
      if(!c) c=p
    } 
    else 
      h=h $1 OFS
  } 
  c && !(p%c) {
    if(h) print h
    print s
    h=s=x
  }
  {
    A[$1]
    s=s $2 OFS
    p=FNR
  }
  END {
    print s
  }
' OFS='\t' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-12-2017
@Ravinder, caution: (!A[$1]) is true if the contents is 0. And has the side effect that it defines the A[$1] element (having no value).
A pure lookup (!($1 in A)) is often preferrable.

---------- Post updated at 14:32 ---------- Previous update was at 13:58 ----------

Here is my awk solution, with comments.
Code:
awk '
BEGIN { format=" %-5s" }
function printA() {
# print the A indices once
  if (!header) {
    header=1
    for (i in A) printf format,i
    printf "\n"
  }
# print the A values
  for (i in A) printf format,A[i]
  printf "\n"
}

($1 in A) {
# repetition detected, print+clear the array
  printA()
  split("",A)
}

# store $2 in $1-indexed array
{ A[$1]=$2 }

# at the end print the array
END { printA() }
' inputfile


Last edited by MadeInGermany; 01-12-2017 at 03:03 PM..
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-12-2017
All of the Gurus, many thanks for your help

---------- Post updated at 03:41 PM ---------- Previous update was at 03:36 PM ----------

Hi Scrutinizer.

Could you be kindly explain what you did in here?

tHanks

---------- Post updated at 03:54 PM ---------- Previous update was at 03:41 PM ----------

Quote:
Originally Posted by Scrutinizer
Another one you could try:
Code:
awk '
  {
    if($1 in A) {
      if(!c) c=p
    } 
    else 
      h=h $1 OFS
  } 
  c && !(p%c) {
    if(h) print h
    print s
    h=s=x
  }
  {
    A[$1]
    s=s $2 OFS
    p=FNR
  }
  END {
    print s
  }
' OFS='\t' file



Could you be kindly explain what you did in here?
# 6  
Old 01-12-2017
Here is a copy of Scrutinizer's script with comments added. I hope this will help you understand how it works. Let us know if there is still something you don't understand after
looking at these comments (along with the awk man page on your system).
Code:
awk '			# Use awk to process the following script:
  {
    if($1 in A) {	# If the value in the 1st field has been seen before
      if(!c) c=p	#   if c has not been set, set it to p.  (Since p is
			#   later set to the previous input line number and $1
			#   will have been seen before and c will not have been
			#   set only when we see the 2nd occurrence of the 1st
			#   value in the 1st field, this sets c to the number
			#   of different values that appear in the 1st field.)
    } 
    else 		# otherwise (the value in the 1st field has not been
			# seen)
      h=h $1 OFS	#   add the 1st field and a field separator to the
			#   header string.
  } 
  c && !(p%c) {		# If c is non-zero and p is an even multiple of c
    if(h) print h	#   if h (the header line) is not an empty string print
			#   it,
    print s		#   print the string of accumulated $2 values, and
    h=s=x		#   clear the header line and the string of accumulated
			#   $2 values.
  }
  {
    A[$1]		# Record that we have seen this $1 value before,
    s=s $2 OFS		# add the current $2 value to the string of accumulated
			# $2 values, and
    p=FNR		# set p to the current input line number.
  }
  END {
    print s		# When we hit EOF on the input file, print the last
			# string of accumulated $2 values.
  }
' OFS='\t' file		# Mark the end of the awk script text, set the output
			# field separator to a tab and name the input file to
			# be processed.


Last edited by Don Cragun; 01-13-2017 at 12:36 AM..
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-13-2017
Don,
Many many thanks Don for your detailed explaining. Excuse my lack of knowledge on this. I have couple of questions ---

1. what is
Code:
c

in this script?
2. what is this this logic for
Code:
If c is non-zero and p is an even multiple of c

in terms of real input file?

Sorry to bother you again on this
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to do column to row in awk

Hi , Can anyone help me suggesting - how to do the below trick with awk Input 120 130 140 210 310 410 645 729 800 Output 120 130 140 (6 Replies)
Discussion started by: Indra2011
6 Replies

2. UNIX for Beginners Questions & Answers

Script to do column to row

Hi , Can anyone help me suggesting - how to do the below trick with awk Input 100 120 130 140 210 310 410 645 729 800 Output (1 Reply)
Discussion started by: Indra2011
1 Replies

3. Shell Programming and Scripting

awk script row to column

Hi.. I have data : Report testing1 20180419 08:00 Report testing2 20180419 07:35 Report testing 20180419 08:01 Source = data1 Report testing4 20180419 08:05 Source = data1 Report testing5 20180419 08:10 Source = data2 Report testing6 20180419 08:01 Report testing7 20180419 08:19... (4 Replies)
Discussion started by: buncit8
4 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Script: Convert row in to column

Hi, i need to convert SG_ERP1 SG_ERP2 SG_ERP3 in to: SG_ERP1 SG_ERP2 SG_ERP3 It's possibile? (16 Replies)
Discussion started by: elilmal
16 Replies

6. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 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. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

column to row convert - script - help

Hi, I have a file named col.txt 1.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000 I should get this 1.000 5.000 2.000 6.000 3.000 7.000 (10 Replies)
Discussion started by: G0Y
10 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question