Computing.Net > Forums > Unix > Awk Variables

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Awk Variables

Reply to Message Icon

Original Message
Name: 18dreams
Date: September 1, 2005 at 12:39:08 Pacific
Subject: Awk Variables
OS: Solaris
CPU/Ram: NA
Comment:

I am writing an awk statement, and need to hold a variable for use until that variable is updated.

Sample data and file - *actual is medical data so can not post

GCAR101 red fast leather
1 3.2 l engine
2 black tires
3 tinted windows
cTrr-02 green slow leather
1 1.4 l enging
2rust
4tR34-1 blue fast no leather
1 no rust

I am trying to create an awk command that will hold a pattern $0 if the length($1)=7, and then put that variable in front of each individual description. Variable should update at each new 7 length first field

Should look like this:

GCAR101 red fast leather ; 1 3.2 l engine
GCAR101 red fast leather ; 2 black tires
GCAR101 red fast leather ; 3 tinted windows
cTrr-02 green slow leather ; 1 1.4 l enging
cTrr-02 green slow leather ; 2 rust
4tR34-1 blue fast no leather ; 1 no rust

Anyone have a suggestion? Trying to a void a loop with test statements as the file is quite large.

Thanks in advance.

Andy


Report Offensive Message For Removal


Response Number 1
Name: 18dreams
Date: September 1, 2005 at 12:40:59 Pacific
Reply:

One error in pre-data. There is a space after each numbered row. I see a few that I forgot to add the space.

Andy


Report Offensive Follow Up For Removal

Response Number 2
Name: 18dreams
Date: September 1, 2005 at 12:51:18 Pacific
Reply:

My actual command:

nawk '{if (length($1)==7); i==$1} { if (length($1)!=7) print i, $0}' OFS=';' $MOD.3 > $MOD.4


Andy


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home



Results for: Awk Variables

awk variable for filename
    Summary: Hi, I want to build a awk variable FILENAME to get the filename of the files that are being read by the shell script. I want to get the filename and put it as the first field in each record in the fil...
www.computing.net/answers/unix/awk-variable-for-filename/7078.html

can I pass an awk variable to the s
    Summary: How can I pass an awk variable to the system command. i have a string variable (like abc.txt) taken from a awk whose extension .txt is to be removed. I am using basename command to remove it. basename...
www.computing.net/answers/unix/can-i-pass-an-awk-variable-to-the-s/7251.html

Unix, Shell Script, awk & variables
    Summary: Hi! I'm trying to use my shell script variables in my awk-line. Is not working! month3 is my variable and i'm aware that I can't use the $-prefix in awk. awk ´$2==$month3{printf ("%16d,%14.1f\n",i+1,\...
www.computing.net/answers/unix/unix-shell-script-awk-amp-variables/5148.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History






Data Recovery Software