help with columns and rows - script


 
Thread Tools Search this Thread
Operating Systems Linux help with columns and rows - script
# 1  
Old 06-16-2011
help with columns and rows - script

Hi everyone,

how can I convert a file with 3375 rows and 6 columns to a file with
1350 rows and 15 columns
by using a script?
Is it possible with awk or something like that?
Any help is much appreciated.

Thanks.
D.
# 2  
Old 06-16-2011
Hmm. Something like this? It saves columns for later and prints them when it has enough.

Code:
awk \
'BEGIN { C=1; MAX=15; }
{
        for(N=1; N<=NF; N++)
        {
                B[C++]=$N;
                if(C > MAX)
                {
                        printf("%s", B[1]);
                        for(M=2; M<=MAX; M++)
                                printf("\t%s", C[M]);
                        printf("\n");
                        C=1;                       
                }
        }
}
END {
    if(c > 1)
    {
        printf("%s", C[1]);
        for(N=1; N<C; N++) printf("\t%s", C[N]);
        printf("\n");
    }
}'

# 3  
Old 06-16-2011
any restrictions on layout/format ? i.e. do you want the first row of o/p to be the first 2.5 rows of input, 2nd row next 2.5 etc ?

Anyway...assuming fields are space seperated this random hack:

Code:
 tr " " "\n" <file | paste -d" " - - - - - | paste -d" " - - -

should work...
# 4  
Old 06-16-2011
Wrench

POSIX shell
Code:
#!/bin/sh
c=0
for f in `cat tmp.file`; do
    printf "$f "
    c=$((c + 1))
    [ $(($c % 15)) -eq 0 ] && echo
done


Last edited by yazu; 06-16-2011 at 01:42 PM.. Reason: The last one didn't work
# 5  
Old 06-16-2011
Quote:
Originally Posted by yazu
POSIX shell
Code:
c=1
for f in `cat tmp.file`; do
  if [ $(($c % 15)) -ne 0 ]; then
    printf "$f "
  else
    echo
  fi
  c=$((c + 1))
done

  1. This is a combo useless use of cat and useless use of backticks. Loading an entire file into a variable to chop it like this is especially dangerous since, if the file is larger than the maximum size of a shell variable, the end will get chopped off.
  2. You shouldn't feed variables into printf's command string unless you can control exactly what they are. It will throw up on or mangle anything with % or \ in it.
  3. It ignores every fifteenth cell.
  4. It may end up printing the last line without an ending line-feed, which may prevent the last line from being read by various UNIX utilities like sed and awk.
  5. It would require a KSH or KSH-like shell, for the $(( )) math syntax.

Code:
c=0
MAX=15
PREFIX=""
# read lines
while read f
do
        # split lines into $1, $2, ...
        set -- $f
        for d in $*
        do
                if [ `expr $c % $MAX` -eq 0 ]
                then
                        # PREFIX being blank prevents extra blank line on the very first line
                        printf "${PREFIX}%s" "$d"
                        PREFIX="\n"
                else
                        printf " %s" "$d"
                fi

                c=`expr $c + 1`       
        done
done < tmp.file

echo


Last edited by Corona688; 06-16-2011 at 01:50 PM..
# 6  
Old 06-16-2011
thanks

Thanks everyone for replying so quickly!

I think yazu's answer is very close, however the script "skips" always the value "-9999.0" which I would like to keep, and does not "keep" the 15th value. Corona88 actually made exactly what I wanted to do.

Thank you all guys for your help!!!Smilie

D.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with script to convert rows to columns

Hello I have a large database with the following structure: Headword=Gloss1;Gloss2;Gloss3 The Glosses are separated by a ; What I need is to reduce the multiple glosses on each row to columns Headword=Gloss1 Headword=Gloss2 Headword=Gloss3 I had written the following script in awk... (5 Replies)
Discussion started by: gimley
5 Replies

2. Shell Programming and Scripting

Help with shell script: selecting rows that have the same values in two columns

Hello, everyone I am beginner for shell programming. I want to print all lines that have the same values in first two columns data: a b 1 2 a a 3 4 b b 5 6 a b 4 6 what I expected is : a a 3 4 b b 5 6 but I searched for one hour in... (2 Replies)
Discussion started by: nengcheng
2 Replies

3. Shell Programming and Scripting

Shell script to convert rows to columns

Hi I have a file having the values like below ---------------------------- .set A col1=”ABC” col2=34 col3=”DEF” col4=”LMN” col5=25 .set A .set B col1=55 col3=”XYZ” col4=”PQR” col5=66 .set B .set C col2=”NNN” (1 Reply)
Discussion started by: Suneel Mekala
1 Replies

4. UNIX for Dummies Questions & Answers

Writing a script to take the average of two columns every 3 rows

I have a dataset with 120 columns. I would like to write a script, that takes the average of every two columns, starting from columns 2 and 3, and moving consecutively in frames of 3 columns, all the way until the last column. The first column in the output file would be the averages of columns... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

Convert columns to rows in perl script

Hi All I want to have a Perl script which convert columns to rows. The Perl should should read the data from input file. Suppose the input file is 7215484 date to date 173.3 A 1.50 2.23 8.45 10.14 2.00 4.50 2.50 31.32 7216154 month to month (3 Replies)
Discussion started by: parthmittal2007
3 Replies

6. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

7. Shell Programming and Scripting

Script - columns to rows

Hi Everyone, I have a file text.cvs, which is a file with columns from excel. The data are separeted with semicolon. The content of the file looks like A B C D E F 1 3 3 4 3 3 2 2 1 2 5 2 5 6 1 1 2 1 Now I wish to write an script which writes the colums A, C and F in... (4 Replies)
Discussion started by: Tob26
4 Replies

8. Shell Programming and Scripting

sql output from multiple rows and columns as variables in a script

This is for an Oracle journal import. I was using a pl/sql package and oracle API's. Oracle added invoker rights to their API's and now my package won't run. I didn't want to use their API's anyway. The only reason i was using pl/sql and the API's (just a package) was to utilize a cursor. How... (2 Replies)
Discussion started by: lmu
2 Replies

9. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

10. Shell Programming and Scripting

shell script required to convert rows to columns

Hi Friends, I have a log file as below siteid = HYD spc = 100 rset = RS_D_M siteid = DEL spc = 200 rset = RS_K_L siteid = DEL2 spc = 210 rset = RS_D_M Now I need a output like column wise as below. siteid SPC rset HYD 100 RS_D_M (2 Replies)
Discussion started by: suresh3566
2 Replies
Login or Register to Ask a Question