Sponsored Content
Top Forums Shell Programming and Scripting rearrange the column names with comma as column delimiter Post 302452542 by durden_tyler on Saturday 11th of September 2010 03:18:20 AM
Old 09-11-2010
Code:
$ 
$ # Example file
$ cat f0.example
col1,col2,col3,col4,col5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
$ 
$ # File with shuffled columns
$ cat f0.shuffled
col5,col3,col2,col4,col1
a5,a3,a2,a4,a1
b5,b3,b2,b4,b1
c5,c3,c2,c4,c1
d5,d3,d2,d4,d1
e5,e3,e2,e4,e1
$ 
$ # Run the Perl script, passing the example file and shuffled data file.
$ # The script reshuffles the header and data of f0.shuffled so that
$ # it matches the order of the header columns of f0.example.
$ perl -lne 'if ($.==1 && $#ARGV == 0) {
             for $k (split/,/) {$x{$k} = $i++}
           } elsif ($.==1) {
             for $k (split/,/) {push @a,$x{$k}; $b[$x{$k}] = $k}
             print join ",",@b; @b=();
           } elsif ($#ARGV == -1) {$i = 0;
             for $k (split/,/) {$n = $a[$i]; $b[$n] = $k; $i++}
             print join ",",@b; @b = ();
           }
           close ARGV if eof;
          ' f0.example f0.shuffled
col1,col2,col3,col4,col5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
c1,c2,c3,c4,c5
d1,d2,d3,d4,d5
e1,e2,e3,e4,e5
$ 
$ # Another file with (differently) shuffled columns
$ cat f1.shuffled
col3,col5,col4,col1,col2
a3,a5,a4,a1,a2
b3,b5,b4,b1,b2
$ 
$ # Run the Perl script, passing the example file and shuffled data file.
$ # The script reshuffles the header and data of f1.shuffled so that
$ # it matches the order of the header columns of f0.example.
$ perl -lne 'if ($.==1 && $#ARGV == 0) {
             for $k (split/,/) {$x{$k} = $i++}
           } elsif ($.==1) {
             for $k (split/,/) {push @a,$x{$k}; $b[$x{$k}] = $k}
             print join ",",@b; @b=();
           } elsif ($#ARGV == -1) {$i = 0;
             for $k (split/,/) {$n = $a[$i]; $b[$n] = $k; $i++}
             print join ",",@b; @b = ();
           }
           close ARGV if eof;
          ' f0.example f1.shuffled
col1,col2,col3,col4,col5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
$ 
$ 
$ 

tyler_durden

Last edited by durden_tyler; 09-11-2010 at 04:25 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change names in a column based on the symbols in another column

If the 4th column has - sign then the names in 3rd column has to change to some user defined names (as shown in output). Thanx input1 1 a aaaaa + 2 b bbbbb + 3 c ccccc + 4 d ddddd + 5 e eeeee + 6 f xxxxx + 8 h hhhhh +... (8 Replies)
Discussion started by: repinementer
8 Replies

2. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

3. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

4. Shell Programming and Scripting

Column to row with delimiter

Hi, i have data in a file f1.txt a b c d and i want to print the above column values in single line with a delimiter, say ',' The output should look like: a,b,c,d I could find rows to columns help online but not vice versa Thanks, -srinivas yelamanchili (4 Replies)
Discussion started by: ysrini
4 Replies

5. Shell Programming and Scripting

Substituting comma "," for dot "." in a specific column when comma"," is a delimiter

Hi, I'm dealing with an issue and losing a lot of hours figuring out how i would solve this. I have an input file which looks like this: ('BLABLA +200-GRS','Serviço ','TarifaçãoServiço','wap.bla.us.0000000121',2985,0,55,' de conversão em escada','Dia','Domingos') ('BLABLA +200-GRR','Serviço... (6 Replies)
Discussion started by: poliver
6 Replies

6. Shell Programming and Scripting

Ignore delimiter within a column

Hi, So I was trying this awk snippet awk -F, '$1 ~ /Match/' File This is my sample file output Name,Age,Nationality,Description Jack,20,American,Tall, caucasian, lean Mary,30,British,Short,white,slim I would expect expected Output to be, when a certain match is met say $1 is... (2 Replies)
Discussion started by: sidnow
2 Replies

7. Shell Programming and Scripting

How to use regex on particular column (Removing comma from particular column)?

Hi, I have pipe separated file which contains some data having comma(,) in it. I want to remove the comma(,) only from particular column without changing data in other columns. Below is the sample data file, I want to remove the comma(,) only from 5th column. $ cat file1 ABC | DEF, HIJ|... (6 Replies)
Discussion started by: Prathmesh
6 Replies

8. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

9. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

10. Shell Programming and Scripting

A cleaner way to rearrange column

Hello, I have some tab delimited text data, index name chg_p chg_m 1 name,1 1 0 2 name,2 1 1 3 name,3 1 0 4 name,4 1 0 5 name,5 1 1 I need to duplicate the "index" column, call it "id" and insert it after the... (8 Replies)
Discussion started by: LMHmedchem
8 Replies
COLRM(1)						    BSD General Commands Manual 						  COLRM(1)

NAME
colrm -- remove columns from a file SYNOPSIS
colrm [start [stop]] DESCRIPTION
The colrm utility removes selected columns from the lines of a file. A column is defined as a single character in a line. Input is read from the standard input. Output is written to the standard output. If only the start column is specified, columns numbered less than the start column will be written. If both start and stop columns are spec- ified, columns numbered less than the start column or greater than the stop column will be written. Column numbering starts with one, not zero. Tab characters increment the column count to the next multiple of eight. Backspace characters decrement the column count by one. ENVIRONMENT
The LANG, LC_ALL and LC_CTYPE environment variables affect the execution of colrm as described in environ(7). EXIT STATUS
The colrm utility exits 0 on success, and >0 if an error occurs. SEE ALSO
awk(1), column(1), cut(1), paste(1) HISTORY
The colrm command appeared in 3.0BSD. BSD
August 4, 2004 BSD
All times are GMT -4. The time now is 05:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy