- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Awk scripting problem - columns
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 02:44 AM
11-28-2002 02:44 AM
I have a data file with a number of records, each with two fields.
One of the fields is an index number, and the other is a quantity value.
I need to put the data in a result file that has a large number of
columns (around 200).
By specifying NF, I can create the number of fields I want in my output
file, but I need to be able to specify that one of the input
values corresponds to a column number and insert its quantity value
in that column.
ie
data column quantity
1 4
4 2
would give a result file (using | delimiter)
4|||2|||
How can I specify the column number using a variable number?
thanks.
Luke
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 03:10 AM
11-28-2002 03:10 AM
Re: Awk scripting problem - columns
I am not sure that awk will do what you want but
http://sparky.rice.edu/~hartigan/awk.html
May help your search.
Try an array in a shell script with the array set to "|" in every value and then just fill up
the variables with number| where relevant.
see man ksh
Regards
Steve Steel
Quote of the moment
-------------------
"We are drowning in information but starved for knowledge."
-- John Naisbitt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 03:19 AM
11-28-2002 03:19 AM
Re: Awk scripting problem - columns
This one fixes number of columns to greatest index :
#! /usr/bin/sh
awk '
BEGIN {
max=0
}
{
t[$1]=$2
if (max < $1)
max=$1
}
END {
for (i=1 ; i <= max ; i++)
if (t[i] == 0)
printf("|")
else
printf("%d|", t[i])
}' $1
and this one fixes number of columns as first argument :
#! /usr/bin/sh
awk -v NB=$1 '
{
t[$1]=$2
}
END {
for (i=1 ; i <= NB ; i++)
if (t[i] == 0)
printf("|")
else
printf("%d|", t[i])
}' $2
if file contains :
1 4
4 8
> script1 infile
4|||8|
> script2 10 infile
4|||8|||||||
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 03:19 AM
11-28-2002 03:19 AM
Re: Awk scripting problem - columns
Not sure if this is what you want, but assuming your output is 20 columns wide:
awk '{a[$1]=$2}END{for (i=1;i<20;i++){printf ("%s|",a[i])};print}' filename
Rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 03:38 AM
11-28-2002 03:38 AM
Re: Awk scripting problem - columns
what about this:
awk '{max=10;for (i=1;i<=max;i++)
{
if ($1==i)
{ printf("%s|",$2)}
else
{ printf ("|")}
if (i==max) printf("\n")
}}' file
file looks like that:
1 4
4 2
3 3
2 5
output looks like that:
4||||||||||
|||2|||||||
||3||||||||
|5|||||||||
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 04:07 AM
11-28-2002 04:07 AM
Re: Awk scripting problem - columns
That is great, thanks.
What I really need, is to get all of them on the same output line. (they are unique index numbers)
ie
instead of
4|||||||
|||2||||
||3|||||
|5||||||
it reads
4|5|3|2||||||
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 05:01 AM
11-28-2002 05:01 AM
Re: Awk scripting problem - columns
Using awk:
cat
I can't guarantee that this version will work, as I don't have access to a unix/linux box at the moment....
Using perl:
perl -wle "open(FILE, $ARGV[0]) || die qq(can't open file); foreach(
print join('|', @fields)"
Regards,
Vincent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 05:05 AM
11-28-2002 05:05 AM
Re: Awk scripting problem - columns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 06:33 AM
11-28-2002 06:33 AM
Re: Awk scripting problem - columns
for this input file:
1 4
4 2
6 14
3 3
9 23
2 5
you can get this output:
|4|5|3|2||14|||23
by running the attached script with the input file as $1
hope it helps,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 06:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 06:52 AM
11-28-2002 06:52 AM
Re: Awk scripting problem - columns
just for info zhe solution in awk
sort file | awk '{zeile[NR]=$0;maxfield=$1}
END {
run=1
for (i=1;i<=maxfield;i++)
{
split(zeile[run],feld)
if ( i == feld[1])
{
printf("%s|",feld[2])
run++
}
else printf("|")
}
}'
perl is much more elegant than this.
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 07:10 AM
11-28-2002 07:10 AM
Re: Awk scripting problem - columns
The perl script from procura
works perfectly although I know
nothing about perl so I hope I dont have to debug it
before I get chance to read up!
:o)
Thanks again
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 07:11 AM
11-28-2002 07:11 AM
Re: Awk scripting problem - columns
# perl -nle'/(\d+)\D+(\d+)/and$x[$1-1]=$2}END{$,="|";print@x' data_file
4|5|3|2
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 07:19 AM
11-28-2002 07:19 AM
Re: Awk scripting problem - columns
# perl -nle'/\D+(\d+)/and$x[$`-1]=$1}END{$,="|";print@x' data_file
4|5|3|2
#
Note that we cannot do
# perl -nle'/\D+/and$x[$`-1]=$'}END{$,="|";print@x' data_file
because $' will end the -e expression
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 08:20 AM
11-28-2002 08:20 AM
Re: Awk scripting problem - columns
a number of datafiles in one go?
And is it possible to maintain the sign of the numbers?
ie keep -4 in the data as -4
thanks
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2002 09:08 AM
11-28-2002 09:08 AM
Re: Awk scripting problem - columns
l1:/tmp 110 > perl -le'$,="|";for(@ARGV){open X,"<$_"or die"$_:$!";@x=();while(
4|5|3|2
5|-12|-99|8000
l1:/tmp 111 > cat data_file1
1 4
4 2
3 3
2 5
l1:/tmp 112 > cat data_file2
3 -99
1 5
4 8000
2 -12
l1:/tmp 113 >
this what you want?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2002 01:08 AM
11-29-2002 01:08 AM
Re: Awk scripting problem - columns
Modification of non-creatable array value attempted, subscript -1 at -e line 1,
Unfortunately I don't know a thing about perl so I have no
idea what the problem is.
The looping I need to do is because I have a directory with
a number of files in of the same format. I need to take each
file and change its format in the way that the first perl script
you gave me did.
I was thinking a simple script along the lines of
for a in `ls
do
perl
done
I tried this loop using the first script you gave (that works
perfectly on the command line) and put it into the loop and
got the same error as the looping perl script...
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2002 01:46 AM
11-29-2002 01:46 AM
Re: Awk scripting problem - columns
4|5|3|2
5|-12|-99|8000
l1:/tmp 103 > perl5.6.1 -le'$,="|";for(@ARGV){open X,"<$_"or die"$_:$!";@x=();while(
4|5|3|2
5|-12|-99|8000
l1:/tmp 104 > perl5.8.0 -le'$,="|";for(@ARGV){open X,"<$_"or die"$_:$!";@x=();while(
4|5|3|2
5|-12|-99|8000
l1:/tmp 105 > perl5.9.0 -le'$,="|";for(@ARGV){open X,"<$_"or die"$_:$!";@x=();while(
4|5|3|2
5|-12|-99|8000
l1:/tmp 106 >
For a change I realy tested the perl examples I posted. What is your perl version? (perl -v)
Show me the failing script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2002 02:28 AM
11-29-2002 02:28 AM
Re: Awk scripting problem - columns
The simple script Im trying to use to run your perl
against all the files in the directory is this:
for a in `ls`
do
perl -le'$,="|";for(@ARGV){open X,"<$_"or die"$_:$!";@x=();while(
done
Also, when I run the perl script (its the version 5.6.1 copy from your last post) on the command line, i still get the error about modification of a non-creatable array.
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2002 02:45 AM
11-29-2002 02:45 AM
Re: Awk scripting problem - columns
In the script you show, you /mix/ the all-in-one solution and the one-by-one solution. To play safe, and readable, do:
for a in `ls` ; do
perl -nle'/(\d+)\D+(\d+)/and$x[$1-1]=$2}END{$,="|";print@x' $a >$a.res
done
Show me the exact error.
Be careful with my posts at the moment. I'm using the latest Opera 7 beta, which removes even more whitespace than the forum already does :/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2002 03:14 AM
11-29-2002 03:14 AM
Re: Awk scripting problem - columns
Modification of non-creatable array value attempted, subscript -1 at -e line 1, <> line 1.
With regards to the whitespace, I am only putting a space at the beginning and
the end, between perl -nle, and print@x' $a.
Should there be more?
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2002 03:51 AM
11-29-2002 03:51 AM
Re: Awk scripting problem - columns
for a in `ls` ; do
echo processing $a ...
perl -nle'/(\d+)\D+(\d+)/&&$1 and$x[$1-1]=$2}END{$,="|";print@x'$a >$a.res
done
If you rather skip the bad lines, do
for a in `ls` ; do
echo processing $a ...
perl -nle'/(\d+)\D+(\d+)/&&$1 or next;$x[$1-1]=$2}END{$,="|";print@x'$a >$a.res
done
If you rather skip the file entirely on these bad input, do
for a in `ls` ; do
echo processing $a ...
perl -nle'/(\d+)\D+(\d+)/&&$1 or exit;$x[$1-1]=$2}END{$,="|";print@x'$a >$a.res
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2002 06:20 AM
12-02-2002 06:20 AM
Re: Awk scripting problem - columns
Thank you all very much for your help and suggestions.
Special thanks to procura.
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2002 12:53 AM
12-18-2002 12:53 AM
Re: Awk scripting problem - columns
a small bug has appeared. It is not maintaining the sign
of the numbers involved.
Any numbers, negative or positive, are made positive
by the perl script.
Is it possible to change the
script so that it maintains
the sign of the data?
Thanks
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2002 01:36 AM
12-18-2002 01:36 AM
Re: Awk scripting problem - columns
for a in `ls` ; do
echo processing $a ...
perl -nle'/(\d+)\D+(-?\d+)/&&$1 and$x[$1-1]=$2}END{$,="|";print@x'$a >$a.res
done
given that columns cannot be negative.
Note: in perl columns *can* be negative, inwhich case they count from the tail: $list[-1] is the last element of @list.