Solved

How to append text to a variable in awk and include all fields?

Posted on 2004-03-31
2
819 Views
Last Modified: 2013-12-26
I am using AIX Unix with the Korn shell.  I have a file that contains part numbers and notes. Each part number may have notes on only one line (record), or 15 or more lines.  I need to have all the notes associated with a part number on one line - I think I've succeeded in doing that.  I need to know how to choose all the fields after the first one (like $2 to NF).

I FIRST INITIALIZE THE FIRST PART NUMBER:
thePARTNUMBER=`awk '{print $1; exit;}' PNOTEsmall.txt`
echo "thePARTNUMBER is $thePARTNUMBER."

THEN I USE AWK TO GO THROUGH EACH RECORD.  IF THE PART NUMBER REMAINS THE SAME ON THE NEXT LINE, THEN I WANT TO APPEND THE NOTES TO THAT PART NUMBER. I want to replace the print statements with something that will append to a variable (partNotes=partNotes+$1 $2 $3 $4 ...etc); then I can print out the part number and all the notes onto one line.  I think I've succeeded in doing some of that, but I  need to know how to choose all the fields instead of using "$1 $2 $3 $4 ...etc", because some lines will have more "fields" (actually just empty spaces in between words) than other lines.  That is my question: how do I choose all the fields after the first one (like $2 to NF) and append it to a variable?  Thank you for your help!!

awk 'BEGIN{theSP=" ";} {
    if($1 == THEpartnumber) {
        partNotes=partNotes $2 theSP $3 theSP $4 theSP $5 theSP;
        BUT I INSTEAD WANT SOMETHING LIKE $2 TO NF
    } else if ($1 != THEpartnumber) {
        print THEpartnumber " " partNotes;
        THEpartnumber=$1;
        partNotes="";
        partNotes=$2 theSP $3 theSP $4 theSP $5 theSP;
    }
}' THEpartnumber=$thePARTNUMBER pnoteCONV.txt > pnoteTILDE.txt

THIS IS A PART OF THE FILE I HAVE TO WORK WITH:
AAD9-14-5                        FOR AIRGO APPLICATION ORDER
AAD9-14-5                        C294501-0103
AA215CC-9                        WHEN INSTALLING NEW DRY VACUUMM PUMPS
AA215CC-9                        USE ONLY S3346-1 OR AIRGO APPROVED
AA215CC-9                        EQUIVALENT GASKETS.  CORK TYPE
AEIO360A1A/8117O                 CLASS I AIRWORTHINESS
AEIO360A1A/8117O                 ENGINE/TC # 1E10
AEIO360A1A/8117O                 AC/TC # A8SO
AEIO360A1A/8117R                 CLASS I AIRWORTHINESS
AEIO360A1A/8117R                 WING/TC # 1E10
AEIO360A1A/8117R                 AC/TC # A8SO

THIS IS HOW I WANT THE FILE TO LOOK:
AAD9-14-5 FOR AIRGO APPLICATION ORDER C294501-0103
AA215CC-9 WHEN INSTALLING NEW DRY USE ONLY S3346-1 OR EQUIVALENT GASKETS. CORK TYPE
AEIO360A1A/8117O CLASS I AIRWORTHINESS  ENGINE/TC # 1E10  AC/TC # A8SO  ENGINES
AEIO360A1A/8117R CLASS I AIRWORTHINESS  WING/TC # 1E10  AC/TC # A8SO  ENGINES
AEIO360A1A/8117RADDITIONAL12345R 9, UN 3166
0
Comment
Question by:psmithphil
2 Comments
 
LVL 9

Accepted Solution

by:
Alf666 earned 500 total points
ID: 10725560
Unfortunately, there is no such thing as "print $2-$NF" in awk. But what you can do is :

for (i=2 ; i<=NF ; i++) { partNotes = partNotes theSP $i ; }

0
 

Author Comment

by:psmithphil
ID: 10726009
Amazing!  That did the trick!  Thank you for your excellent help!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question