Solved

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

Posted on 2004-03-31
2
821 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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).
Suggested Courses

632 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