Solved

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

Posted on 2004-03-31
2
820 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

739 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