![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| du from list with du of list total | Movomito | Shell Programming and Scripting | 3 | 05-04-2008 09:33 PM |
| list of unmatched columns | mohan705 | Shell Programming and Scripting | 3 | 12-12-2007 10:37 AM |
| Row to Columns | vskr72 | UNIX for Dummies Questions & Answers | 4 | 03-21-2007 09:53 AM |
| how to generate a random list from a given list | mskcc | Shell Programming and Scripting | 3 | 05-30-2006 03:30 AM |
| Comparing a distinct value in 1 list with another list | manualvin | Shell Programming and Scripting | 6 | 06-22-2004 06:42 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
List to columns and awk help
Hi I'm new to this forum and I'm a beginner when it comes to shell programming and awk programming. But I have the following problem:
I've a list like this: 1 2 3 4 5 6 7 8 Either from a file or output from a command. What I would like to do is to arrange these values into x columns with values in each column like this: 1 2 3 4 5 6 7 8 Where x specifies how many columns I would like. Then I would like to add all the values from column 1 and divide it with the numbers of rows (in this case 2). forgot something: this is for a bourne shell script. Is it possible to have x as a variable input when running the script? Or is it possible to, instead of making columns, to add every x element in the list and then take an average? Last edited by baghera; 08-26-2007 at 01:09 PM.. |
|
||||
|
add every 'x' element
Code:
awk -v var=4 -f sample.awk inputfile Code:
(NR % var ) == 0 { sum+=$0; cnt++}
END {print sum, cnt }
If you want the count starting from the first column in the list, change the sample.awk to Code:
BEGIN { row_cnt=1 }
(NR % row_cnt ) == 0 { sum+=$0; cnt++; row_cnt+=var }
END {print sum, cnt }
|
|
||||
|
cat file | xargs -n <number of items in a row>
Code:
"/home/tdreader" > cat t.txt 1 2 3 4 5 6 7 8 9 12 12 32 5 66 56 343 8 875 434 0 "/home/tdreader" > cat t.txt | xargs -n 3 1 2 3 4 5 6 7 8 9 12 12 32 5 66 56 343 8 875 434 0 go for this Code:
cat filename | tr '\n' ' ' | xargs -n 3 |
|
||||
|
I really appreciate the help.
I like the xargs thing. Is it then possible to add each element in a column and divide it by the numbers of elements in the column. I've used this command: awk '{ sum+=$1/2 }{ sum1+=$2/2} END { print sum sum1 }' But $1/2 where the 2 should be a variable (the number of elements in the column). Also ranj@chn I didn't get your script to work, I must be doing something wrong. I just copied what you wrote and tried to run it with: awk -v var=2 -f reader2.awk testfile.txt But I only get errors. And I also tried to run ahmedwaseem2000's program but I didn't get that to work either. I really sorry but I'm a real "noob" at this. But the help you are giving me are invaluable. |
|
||||
|
post the errors
Do post the errors and the Unix box that you are connected to - o/p of
Code:
uname -a |
|
||||
|
Quote:
Quote:
YOU NEED TO ASSIGN VALUE OF "INPUT" VARIABLE before running the code else it will give you the "division by zero in modulus" like run INPUT=4 before running the code. |
| Sponsored Links | ||
|
|