Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-03-31
2
Medium Priority
?
823 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 2000 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

Technology Partners: 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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

721 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