Sponsored Content
Top Forums Shell Programming and Scripting How do I transpose a of results ? Post 302419396 by aigles on Friday 7th of May 2010 04:32:47 AM
Old 05-07-2010
Try and adapt the following AWK program :
Code:
awk '

BEGIN {
   id1 = "Id1";
   id2 = "Id2";
    dx = "dE/dx";
    dy = "dE/dy";
    dz = "dE/dz";
   pattern_dE = "^dE\\/d[xyz]";
}

function printAtom() {
   if (atomCount != 0) {
      print "Atom dE/dx dE/dy dE/dz";
      for (i=1; i<=atomCount; i++) {
         print atom[i, id1], atom[i, id2], atom[i, dx], atom[i, dy], atom[i, dz];
      }
      atomCount = 0;
   }
}

{ gsub(/D-[^ -]*/, "& ") }

/^ATOM/ {
   printAtom();

   atomCount = (NF - 1) / 2;
   for (i=1; i<=atomCount; i++) {
      atom[i, id1] = $(i*2);
      atom[i, id2] = $(i*2+1);
   }
}

$0 ~ pattern_dE {
   for (i=1; i<=atomCount; i++) {
      atom[i, $1] = $(i+1);
   }
}

END {
   printAtom();
}

' wan.txt

Output :
Code:
Atom dE/dx dE/dy dE/dz
1 ca 0.2057422D-01 -0.7179106D-02 -0.5600872D-02
2 o 0.2463722D-01 -0.1554542D-01 0.3110649D-01
3 h -0.1068047D-01 0.1016889D-01 -0.4088230D-02
4 h -0.1495280D-01 0.3268502D-02 -0.2295107D-01
5 o -0.3725362D-02 -0.4888578D-01 -0.2832048D-01
Atom dE/dx dE/dy dE/dz
6 h 0.9525615D-02 0.3902392D-01 0.2086767D-01
7 h -0.9503037D-02 0.1220264D-01 0.1308971D-01
8 o 0.1689507D-01 0.5837539D-01 0.4212211D-01
9 h 0.7277895D-03 -0.1350591D-02 -0.3857783D-02
10 h -0.2111557D-01 -0.6110345D-01 -0.3506507D-01
Atom dE/dx dE/dy dE/dz
11 o -0.1542481D-01 -0.1334465D-01 0.2977948D-01
12 h 0.9872019D-02 0.3666496D-02 -0.1278534D-01
13 h -0.2025238D-02 0.1088555D-01 -0.1272541D-01
14 o 0.6888561D-02 0.2137170D-01 0.4976557D-02
15 h 0.1057477D-01 -0.6013097D-02 -0.5361850D-03
Atom dE/dx dE/dy dE/dz
16 h -0.1607803D-01 -0.1357829D-01 -0.6053460D-02
17 o -0.4466439D-02 -0.2266521D-01 -0.3893747D-02
18 h -0.8633477D-02 0.4109669D-01 -0.1146559D-01
19 h 0.1463235D-01 -0.4789354D-02 0.1447851D-01
20 o -0.1256357D-01 -0.3342545D-01 0.2771696D-01
Atom dE/dx dE/dy dE/dz
21 h 0.1159822D-02 0.1192183D-01 -0.3776217D-01
22 h 0.6070351D-02 0.1009477D-01 0.4180746D-02
23 o 0.8284857D-02 -0.5001876D-02 0.5491223D-02
24 h -0.6641931D-03 -0.2469536D-02 0.2433015D-02
25 h -0.1313546D-01 0.1214988D-01 -0.8376573D-02
Atom dE/dx dE/dy dE/dz
26 o 0.1103057D-01 -0.1885375D-01 0.2716471D-01
27 h -0.1328518D-01 0.2136716D-01 -0.3088887D-01
28 h 0.9606614D-02 -0.2015923D-03 -0.2871229D-02
29 o 0.3365380D-02 0.1388877D-01 -0.8382806D-03
30 h 0.4944141D-02 -0.5653677D-02 0.3075100D-02
Atom dE/dx dE/dy dE/dz
31 h -0.1252600D-01 -0.8830054D-02 0.1604802D-02

Jean-Pierre.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

transpose command

I need to transpose data from one row to one column. If there is any idea to do it would be helpful. example of input data 314.16177368164102, 314.71533203125, 315.208740234375 wantted output data 314.16177368164102 314.71533203125 315.208740234375 Thank you so much for any advance... (3 Replies)
Discussion started by: su_in99
3 Replies

2. Shell Programming and Scripting

How do I transpose a column of results to a row

Hi, Can anyone advise me what command I could use to display the results of the following command as a row. Thanks Gareth (6 Replies)
Discussion started by: m223464
6 Replies

3. Shell Programming and Scripting

Transpose

TRANSPOSE -------------------------------------------------------------------------------- i have a file with recurring fields Start A 1 B 2 C 3 D 4 E 5 End Start A 11 B 12 C 23 D 25 E 21 (1 Reply)
Discussion started by: aravindj80
1 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. Shell Programming and Scripting

Transpose Second column only

Hi Folks, My input file is like this cat input abcd:efgh:jklm 123,456,67,78,89,90 hi:kil:op 76,78,12,3456, unix:linux:shell:bash 111,111 My expected output abcd:efgh:jklm hi:kil:op unix:linux:shell:bash 123 76 111 456 78 111 67 12 78 3456 89 90 (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Find and transpose

Dear All I was wondering how to resolve an issue that I met during my analysis. In particular I have a file like this(tab separated): factor1 element1 chr1 309343146 330945480 1 protein_coding geneA factor2 element2 chr2 309350853 309603230 1 protein_coding geneA factor3 element3 chr3... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

7. Shell Programming and Scripting

Transpose using awk

Hi Friends, Very urgent requirement please do needful ASAP.. Input: |1||1|1||1|3||3|2||2|4||4|2||2|3||3|NA||0|5||5|NA||0|4||4|3||3 output: |1||1 |1||1 |3||3 |2||2 |4||4 |2||2 |3||3 |NA||0 |5||5 (4 Replies)
Discussion started by: bharat1211
4 Replies

8. UNIX for Dummies Questions & Answers

Transpose File

Have various files like this: InSlot=0x00000001 InPort=0x00000000 Inref=0x0000002f InSID=0x00000001 OutSlot=0x00000001 OutPort=0x00000002 Outref=0x00000000 OutSID=0x0000000b OutUName_2=14 InSlot=0x00000001 InPort=0x00000000 Inref=0x000001a8 InSID=0x00000001 OutSlot=0x00000001... (5 Replies)
Discussion started by: K@rlos
5 Replies

9. UNIX for Beginners Questions & Answers

Transpose the data

Hi All, I have sort of a case to transpose data from rows to column input data Afghanistan|10000|1 Albania|25000|4 Algeria|25000|7 Andorra|10000|4 Angola|25000|47 Antigua and Barbuda|25000|23 Argentina|5000|3 Armenia|100000|12 Aruba|20000|2 Australia|50000|2 I need to transpose... (3 Replies)
Discussion started by: radius
3 Replies

10. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies
AWK(1)							      General Commands Manual							    AWK(1)

NAME
awk - pattern scanning and processing language SYNOPSIS
awk [ -Fc ] [ prog ] [ file ] ... DESCRIPTION
Awk scans each input file for lines that match any of a set of patterns specified in prog. With each pattern in prog there can be an asso- ciated action that will be performed when a line of a file matches the pattern. The set of patterns may appear literally as prog, or in a file specified as -f file. Files are read in order; if there are no files, the standard input is read. The file name `-' means the standard input. Each line is matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern. An input line is made up of fields separated by white space. (This default can be changed by using FS, vide infra.) The fields are denoted $1, $2, ... ; $0 refers to the entire line. A pattern-action statement has the form pattern { action } A missing { action } means print the line; a missing pattern always matches. An action is a sequence of statements. A statement can be one of the following: if ( conditional ) statement [ else statement ] while ( conditional ) statement for ( expression ; conditional ; expression ) statement break continue { [ statement ] ... } variable = expression print [ expression-list ] [ >expression ] printf format [ , expression-list ] [ >expression ] next # skip remaining patterns on this input line exit # skip the rest of the input Statements are terminated by semicolons, newlines or right braces. An empty expression-list stands for the whole line. Expressions take on string or numeric values as appropriate, and are built using the operators +, -, *, /, %, and concatenation (indicated by a blank). The C operators ++, --, +=, -=, *=, /=, and %= are also available in expressions. Variables may be scalars, array elements (denoted x[i]) or fields. Variables are initialized to the null string. Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. String constants are quoted "...". The print statement prints its arguments on the standard output (or on a file if >file is present), separated by the current output field separator, and terminated by the output record separator. The printf statement formats its expression list according to the format (see printf(3)). The built-in function length returns the length of its argument taken as a string, or of the whole line if no argument. There are also built-in functions exp, log, sqrt, and int. The last truncates its argument to an integer. substr(s, m, n) returns the n-character sub- string of s that begins at position m. The function sprintf(fmt, expr, expr, ...) formats the expressions according to the printf(3) for- mat given by fmt and returns the resulting string. Patterns are arbitrary Boolean combinations (!, ||, &&, and parentheses) of regular expressions and relational expressions. Regular expressions must be surrounded by slashes and are as in egrep. Isolated regular expressions in a pattern apply to the entire line. Regu- lar expressions may also occur in relational expressions. A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines between an occurrence of the first pattern and the next occurrence of the second. A relational expression is one of the following: expression matchop regular-expression expression relop expression where a relop is any of the six relational operators in C, and a matchop is either ~ (for contains) or !~ (for does not contain). A condi- tional is an arithmetic expression, a relational expression, or a Boolean combination of these. The special patterns BEGIN and END may be used to capture control before the first input line is read and after the last. BEGIN must be the first pattern, END the last. A single character c may be used to separate the fields by starting the program with BEGIN { FS = "c" } or by using the -Fc option. Other variable names with special meanings include NF, the number of fields in the current record; NR, the ordinal number of the current record; FILENAME, the name of the current input file; OFS, the output field separator (default blank); ORS, the output record separator (default newline); and OFMT, the output format for numbers (default "%.6g"). EXAMPLES
Print lines longer than 72 characters: length > 72 Print first two fields in opposite order: { print $2, $1 } Add up first column, print sum and average: { s += $1 } END { print "sum is", s, " average is", s/NR } Print fields in reverse order: { for (i = NF; i > 0; --i) print $i } Print all lines between start/stop pairs: /start/, /stop/ Print all lines whose first field is different from previous one: $1 != prev { print; prev = $1 } SEE ALSO
lex(1), sed(1) A. V. Aho, B. W. Kernighan, P. J. Weinberger, Awk - a pattern scanning and processing language BUGS
There are no explicit conversions between numbers and strings. To force an expression to be treated as a number add 0 to it; to force it to be treated as a string concatenate "" to it. AWK(1)
All times are GMT -4. The time now is 02:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy