List to columns and awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List to columns and awk help
# 8  
Old 08-27-2007
Quote:
Originally Posted by ahmedwaseem2000
what is the error you are getting? its working perfectly fine for me. see below


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.
I'm running this command:

awk -v INPUT=2 -f test.awk test

isn't this correct. Also I just copied your code into test.awk and test is a file with numbers, like so:

1
2
3
4
5
6

The error I get is this:

awk: syntax error at source line 1 source file test.awk
context is
awk -v >>> INPUT= <<< $INPUT '
awk: bailing out at source line 18

Right now I'm sitting in Mac OS X, but I need it to run on Solaris. Maybe this is a part of the problem? Also I see that the last row in the first column you get the average value of the column above, that is really nice. Could it be done for every column?
# 9  
Old 08-27-2007
Quote:
Originally Posted by baghera
I'm running this command:

awk -v INPUT=2 -f test.awk test

isn't this correct. Also I just copied your code into test.awk and test is a file with numbers, like so:

1
2
3
4
5
6

The error I get is this:

awk: syntax error at source line 1 source file test.awk
context is
awk -v >>> INPUT= <<< $INPUT '
awk: bailing out at source line 18

Right now I'm sitting in Mac OS X, but I need it to run on Solaris. Maybe this is a part of the problem? Also I see that the last row in the first column you get the average value of the column above, that is really nice. Could it be done for every column?

copy the below code to the file test.awk and run like this ./test.awk

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

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

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

}

END { 
 op = sum/count
print op }' test

# 10  
Old 08-27-2007
Quote:
Originally Posted by ahmedwaseem2000
copy the below code to the file test.awk and run like this ./test.awk

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

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

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

}

END { 
 op = sum/count
print op }' test

Alright I ran what you suggested and it worked. How can you make it so that you can provide a variable for how many columns you would like, as you showed me in in the previous program? And then how to take to average value of each column and make a nice looking formated outprint?
# 11  
Old 08-28-2007
My previous code was parameterized. $INPUT is the variable that defines how many columns to be created.

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

# 12  
Old 08-28-2007
Quote:
Originally Posted by ahmedwaseem2000
My previous code was parameterized. $INPUT is the variable that defines how many columns to be created.

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

So I changed to INPUT=$INPUT like in the original, but I still isn't able to execute the program, the command I'm running is:

awk -v INPUT=2 -f test.awk

and changed the filename to test.txt (which is my list). But is it possible to also make the the input-file a variable just like INPUT? And also the issue with adding each element in a column and then dividing it with the number of elements.
# 13  
Old 08-28-2007
you need to run the script as i have shown before. Yes even the filename can be parameterized. $Filename is the filename

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 }' $(echo $Filename)

# 14  
Old 08-28-2007
Is there any nice command with awk to count the elements in an column? Not an entire program but some nice short way to count the elements like so:

1
2
3
4

Then the "counter" should say 4 since there is 4 elements in the column.
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