Operating System - HP-UX
1831207 Members
3216 Online
110021 Solutions
New Discussion

Awk: split into fields, and then split again

 
SOLVED
Go to solution
Robert A. Pierce
Frequent Advisor

Awk: split into fields, and then split again

Good day!

I have text files formatted like this:

===================================================
Logging begins at: [2004.05.31 00:24:20]
r [2004.05.31 00:24:34] (348) reason1: more text: foofoo STRING2: [9.8.7.5]
r [2004.05.31 00:24:47] (325) reason1: more text: var bar car STRING: txt.info.dot [4.3.2.1]
r [2004.05.31 00:25:20] (476) other reason text: variable lenght ZZZZZ: stuff.i.want.now [5.6.7.8]
r [2004.05.31 00:25:20] (391) reason1: some other text: foo BAR: stuff.wanted.txt [1.2.3.4] various text of various length
[and so on]

I can pull out the portion in the second square brackets with AWK like this:

> BEGIN { FS="[][]" }
> {print $4}

(NB: FS="[[]]" does not work!)

Now, what I'd like to do is split the $3 portion, colon-delimited, and get the last field. In the text above, that would be:

(blank)
txt.info.dot
stuff.i.want.now
stuff.wanted.txt

Then I'd like to print $4, (new_$3) like this

[1.2.3.4] (stuff.wanted.txt)

I've looked in the slender loris book, comp.lang.awk, and here, but either I am not searching properly or I'm to ignorant to understand what I'm reading.

Awk preferred; perl answers are nice, but no "one-liners," please. :-)

Thanks,

Rob
9 REPLIES 9
Geoff Wild
Honored Contributor

Re: Awk: split into fields, and then split again

Can't you just set the delimeter?

awk -F:

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
curt larson_1
Honored Contributor
Solution

Re: Awk: split into fields, and then split again

split your line into an array

split($0,a,":");

then split array element

split(a[4],b,"[");

print b[1];
Robert A. Pierce
Frequent Advisor

Re: Awk: split into fields, and then split again


So, would you do something like

BEGIN { FS="[][]" }

{
print $4
temp=$3
FS=:
print "("$NF")\n"
}

I wasn't able to get that to work.

what am I missing?
curt larson_1
Honored Contributor

Re: Awk: split into fields, and then split again

of course it does seem to be easier if you use colon as your field seperator

awk -F: '{
split($4,a,"[");
printf("[%s,%s\n",a[2],a[1]);
}'
curt larson_1
Honored Contributor

Re: Awk: split into fields, and then split again

robert,

BEGIN { FS="[][]" }

{
print $4
temp=$3
FS=:
print "("$NF")\n"
}

I wasn't able to get that to work.

what am I missing?

no semicolon at the end of statement
print $4;
temp=$3;
etc
curt larson_1
Honored Contributor

Re: Awk: split into fields, and then split again

robert,

not sure what your trying to do with this:
print "("$NF")\n"

print number of fields:
print NF;

print the last field:
print $NF;

usually i think what you want is to do this
printf("%s\n",$NF);
Robert A. Pierce
Frequent Advisor

Re: Awk: split into fields, and then split again

Curt,

BEGIN { FS="[][]" }

{
print $4

sizeOfArray =split($3,name,":");
print name[sizeOfArray];

}

Works mostly!

On some records it's returning too much; let me see why . . . .
Robert A. Pierce
Frequent Advisor

Re: Awk: split into fields, and then split again


I'm not using the colon at first, because there is a variable number of colons involved.

The constants are: the SECOND square-bracketed field, and the LAST colon-delimited field prior to that 2nd [] field.

Robert A. Pierce
Frequent Advisor

Re: Awk: split into fields, and then split again

This works:


BEGIN { FS="[][]" }

match($1,"^r") >0 {

sizeOfArray =split($3,name,":");
print $4" ("name[sizeOfArray]")";

}

The split() is what did it.

Thanks to all who replied!

Rob