Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 98
sum13 challenge 24 77
CSS HELP 6 74
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 50
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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now