List to columns and awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List to columns and awk help
# 1  
Old 08-26-2007
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 02:09 PM..
# 2  
Old 08-27-2007
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  
Old 08-27-2007
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  
Old 08-27-2007
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  
Old 08-27-2007
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  
Old 08-27-2007
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  
Old 08-27-2007
Quote:
Originally Posted by baghera
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Portable way to print list in columns

so if i have a list of file names, on linux system, we can use the column command to split them up into columns. sadly, the "columns" command does not exist on some OSes. so i found that the pr command can also work. but, pr tends to truncate the names. There's a way around that on... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

4. UNIX for Dummies Questions & Answers

Pick out columns according to list of column names

Hi Everyone: I'm new to linux and I was wondering what the best way to approach the following problem was. I have 2 files: File A: ID123 ID234 ID456 File B: ID123 ID234 ID345 ID456 A B C D E F G H I J K L Based on the list of IDs in File A, I want to output only the... (3 Replies)
Discussion started by: chongm88
3 Replies

5. Shell Programming and Scripting

break from a single list into multiple columns

Hi Guys, I am prety new to the hell scripting world. I am running some grep/cut commands and extracting from a csv file into a list. But the final product I need is that the whole list that I now have has to be broken and separated into columns. Say what I now have extracted is a list of... (6 Replies)
Discussion started by: h_rishi
6 Replies

6. Shell Programming and Scripting

Converting a list to X columns of csv (& wrapping a command around it)

Hi All, Random question, how would you convert a data file from a list like so: 12345 12346 12347 12348 12349 12350 ... <snip 100+ lines> ... to comma separated X columns across: 12345,12346,12347 12348,12349,12350 Why would you want to do this? The background to this is a... (2 Replies)
Discussion started by: craigp84
2 Replies

7. UNIX for Dummies Questions & Answers

Creating columns from a list

I have a list below, how can I have things separated nicely in columns mv browseDir.tcsh browseDir.csh mv checkSRDist.tcsh checkSRDist.csh mv create-data-tinv.tcsh create-data-tinv.csh mv createDocs.tcsh createDocs.csh mv createMisfit.tcsh createMisfit.csh mv createModel.tcsh... (4 Replies)
Discussion started by: kristinu
4 Replies

8. Shell Programming and Scripting

Deleting columns by list or file

Dear specialists out there, please help a poor awk newbie: I have a very huge file to process consisting of 300000 columns and 1500 rows. About 20000 columns shall be deleted from that file. So it is clear, that I can't do this by writing down all the columns in an awk command like $1, $x etc.... (5 Replies)
Discussion started by: flxms
5 Replies

9. Shell Programming and Scripting

how to AWK columns from $1 to $5 without $2 $3 $4

hello cant find a way to make something like: awk '{print $1 - $5}' somefile which is printing $1 $2 $3 $4 $5 should make an array or something? i just dont wanna write $1 $2 $3 $4 $5 to awk input i need to use from $1 to $5 and print them all and then i need to swith to: from $6 to $10 (3 Replies)
Discussion started by: tip78
3 Replies

10. Shell Programming and Scripting

list of unmatched columns

Hi , I have two files want to compare and list of column values and postion which are not matched between two files,I can use diff but it will return rows from two files which are matched and unmatched columns.I wrote the below script but not working. f1=$1 f2=$2 for i in 1 do file1=`cat... (3 Replies)
Discussion started by: mohan705
3 Replies
Login or Register to Ask a Question