Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stringing output to same line and multiple lines if NF>x 1

Status
Not open for further replies.

cconsun

Technical User
Apr 19, 2002
4
US
Hello,

I have spent some time reading AWK ref mtl and searching your FAQ, but no luck yet.
I have a one liner used to pull $1 from my INPUT file and write to single line of OUTPUT file seperating with ",".

Code:
awk '{ORS=","} ("$1"!="Case") {print $1}' INPUT > OUTPUT

This works great, but I would like my OUTPUT to only have 24 entries of $1 then go to /n and continue writing.

Help please,
Chris
 
cconsum:

If I'm interpreting correctly what you want, you should be using the ordinal number of records, NR, and not NF, the number of fields. NR automatically updates by 1 for each record awk reads.

Regards,

Ed
Schaefer
 
Now the next question is, how do I include this in the awk line to have it only write 24 entries on each line?
My awk now writes all entries to 1 line. I need to break it up to write 24 entries per line.
 
Chris:

This is quick, dirty, and ugly: Read your 24 items into an awk array, print them, reinit the counters, and keep reading and writing until the end.

Like I said, ugly:

# my array doesn't start at 0, maybe it should
nawk ' BEGIN { acnt = 24
ind = 0 }
{
ind++
if (ind > acnt)
{
for (i=1; i < ind; i++)
printf(&quot;%s &quot;, aa)
printf(&quot;\n&quot;)

ind=1
}
aa[ind]=$2
} END {
# anything stored in the array, print it out
if (ind >= 1)
{
for (i=1; i < (ind+1); i++)
printf(&quot;%s &quot;, aa)
}
} ' < data.file
 
Ed,

Please excuse my ignorance. I am fairly new to AWK.
I am attempting to use the code supplied. Am getting the following:
nawk: can't read value of aa; it's an array name.
I am either not explaining what I want the end result to look like, or I am not understanding how to provide input for array. Probably a little of both.

Let me start over....
INPUT file looks like this:
Case ID Severity Hours Open Serial #
62813949 1 2315.41 13818
62905955 1 27.01 ft14510025

OUTPUT file looks like this:
Case,62813949,62905955

Now, I take these Case ID's and put them into an application to close them out. Problem is, I can only input 24 cases in a single line into the application.
So, when I get a report with more than 24 cases, I have to manually split them off and enter them on to seperate lines.
I want my awk to do this for me.
How do I do this?

C
 
olded was in a little hurry obviously, his idea should work however. ;)
Fix:
replace the aa in the printf with aa.

or try this: same idea.
awk ' {
while (getline) {
r++ ; array[r] = $0
if ((r % 24) == 0) {
x++
for (i in array) {
print array,&quot;AT&quot;,x >> &quot;filename&quot;
}
delete array
}
}
}' sourcefilename
 
olded was in a little hurry obviously, his idea should work however. ;)
Fix:
replace the aa in the printf with aa.

or try this: same idea.
 awk ' {
 while (getline) {
   r++ ; array[r] = $0
   if ((r % 24) == 0) {
       x++
       for (i in array) {
         print array,&quot;AT&quot;,x >> &quot;filename&quot;
        }
      delete array
       }
    }
}' sourcefilename

Look at me: open mouth->insert foot.
Disabling tgml helps.
 
Forget it. Nothing works.
the counter &quot;i&quot; should be put inside brackets
&quot;&quot;, (without quotes) at the end of both my array,
oldeds aa.

 
Hi cconsum-

Try this one-liner.


awk '{ NR==1 ; if( NR % 24 != 0 ) printf(&quot;%s,&quot;,$1) ; else print $1 }' input > output

HTH



flogrr
flogr@yahoo.com

 
Hi:

flogrr: Good one!

I must apologize for my code snippet. When I pasted into the reply, the brackets we're inadvertly dropped. I went back and checked my code and they were there.

Just for grins, I removed the brackets, and, of course, you get a &quot;can't read value of array&quot; error.

Thanks for keeping me honest.

Regards,

Ed
 
Here's a sed solution since I like the challenge. flogrr's solution is at least twice as fast, however. The increment feature here should be credited to Bruno Haible.

Code:
#! /usr/bin/sed -f
x
s/.*/1/
x
b proc
:loop
$b end
x
:d
s/9\(_*\)$/_\1/
td
s/^\(_*\)$/0\1/
s/8\(_*\)$/9\1/
s/7\(_*\)$/8\1/
s/6\(_*\)$/7\1/
s/5\(_*\)$/6\1/
s/4\(_*\)$/5\1/
s/3\(_*\)$/4\1/
s/2\(_*\)$/3\1/
s/1\(_*\)$/2\1/
s/0\(_*\)$/1\1/
s/_/0/g
x
N
:proc
s/\(.*\n\)[     ]*\(.*\)/\1\2/
s/[     ][      ]*.*//
x
/24/{
  x
  b end
}
x
b loop
:end
s/\n/,/g

Cheers,
ND [smile]
 
Thanks to all that replied on this.
I kept getting errors even after using the [] in the array.

flogrr - thanks for the one liner, this is what I wound up going with. Initially, I got an error with record 25, but switched to gawk and it works just like I need it to.

C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top