The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 08-26-2007
baghera baghera is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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..
  #2 (permalink)  
Old 08-27-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
add every 'x' element

Code:
awk -v var=4 -f sample.awk inputfile
where sample.awk is
Code:
(NR % var ) == 0 { sum+=$0; cnt++}
END {print sum, cnt }
This adds every 4'th element is the list.

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 }
  #3 (permalink)  
Old 08-27-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
Try this:

Code:
awk -v INPUT=$INPUT '
BEGIN { ORS=""}
{

print $0 " "
if (( NR % INPUT ) == 0) {
print "\n"
count++
}

if (( NR % INPUT ) == 1) 
sum += $0

}

END { 
 op = sum/count
print op }' Filename
  #4 (permalink)  
Old 08-27-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 559
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
If it is a file with one number in a line,
go for this

Code:
cat filename | tr '\n' ' ' | xargs -n 3
  #5 (permalink)  
Old 08-27-2007
baghera baghera is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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.
  #6 (permalink)  
Old 08-27-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
post the errors

Do post the errors and the Unix box that you are connected to - o/p of
Code:
uname -a
That could help identifying the issue. You must use 'nawk' instead of awk on Solaris and 'gawk' if its Linux.
  #7 (permalink)  
Old 08-27-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
Quote:
Originally Posted by baghera View Post
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.
what is the error you are getting? its working perfectly fine for me. see below


Quote:
1 2 3 4
5 6 7 8
3
3 is the (1 + 5 )/2=3

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
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0