Sponsored Content
Operating Systems Linux help with columns and rows - script Post 302531349 by Corona688 on Thursday 16th of June 2011 12:37:14 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
pfspad(1)						      General Commands Manual							 pfspad(1)

NAME
pfspad - Add borders to a frame in PFS stream SYNOPSIS
pfspad [--left <columns>] [--right <columns>] [--top <rows>] [--bottom <rows>] [--width <new_width>] [--height <new_height>] [--R <val>] [--G <val>] [--B <val>] [--Y <val>] [--help] DESCRIPTION
Add borders to each frame in PFS stream. User specifies size of left, right, top and bottom border or size of an output image. Color of borders can be specified as well. OPTIONS
--left <columns>, -l <columns> Width in pixels of the left border. --right <columns>, -r <columns> Width in pixels of the right border. --top <rows>, -t <rows> Height in pixels of the top border. --bottom <rows>, -b <rows> Height in pixels of the bottom border. --width <new_width>, -W <new_width> Width of an output image. Note that --width can be mixed with either --left or --right option. --height <new_height>, -H <new_height> Height of an output image. Note that --height can be mixed with either --top or --bottom option. --R <val>, -R <val>, --G <val>, -G <val>, --B <val>, -B <val> Color of borders in RGB mode. Default color is black, and if some of the components is not specified, its value is set to 0.0. --Y <val>, -Y <val> Color of borders in luminance mode. --help, -h Print a list of commandline options. EXAMPLES
pfsin image.hdr | pfspad --left 50 --bottom 20 | pfsout out.hdr Add 50 columns to the left and 20 rows to the bottom side of image.hdr. Color of borders is default black. pfsin image.hdr | pfspad --top 20 --height 500 --width 400 --R 1.0 | pfsout out.hdr Add 20 rows to the top side of image.hdr and create output image out.hdr 500 pixels in height and 400 pixels in width. Color of bor- ders is set to red. SEE ALSO
pfsin(1) pfsout(1) BUGS
Please report bugs and comments to Dorota Zdrojewska <dzdrojewska@wi.ps.pl>. pfspad(1)
All times are GMT -4. The time now is 05:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy