1753742 Members
4828 Online
108799 Solutions
New Discussion юеВ

Re: awk stumper

 
SOLVED
Go to solution
Craig Rants
Honored Contributor

awk stumper

I am trying to perform a task in awk and have run into a block wall. (Any responses on how to do this in sed will get negative points, I can already do it sed...)

I want to be able to find my string in a file, then use the field number as a basis for my subsitition. I have the my command for matching, but I can't get the sub to work. Any help would be appreciated.

awk 'match($0,"u_maxtries"){FS=":";print substr ($0,0)}' root

Craig
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
8 REPLIES 8
Steven Mertens
Trusted Contributor

Re: awk stumper

hi

I'm not an awk freak so the chance is big i'm
wrong but substr doesnt do a substitution i
think. It is the function sub that can do
this.

you can also use awk '/pattern/' file to
find a string.

rgds.

STeven
Hai Nguyen_1
Honored Contributor

Re: awk stumper

Craig,

Can you give us an example of your input line and its associated output line?

Hai
Steven Mertens
Trusted Contributor

Re: awk stumper


maybe that will do it ? :

awk 'match($0,"u_maxtries"){FS=":";sub($0,0); print'} root

but it will replace the whole line that
contains the word u_maxtries in 0
Craig Rants
Honored Contributor

Re: awk stumper

This is the output from the command.

:u_unsuclog#1026418505:u_unsuctty=console:u_maxtries#9:u_lock@:
I want to be able to determine the field that u_maxtries is in and then do my substitution from that.

Yes I know the sub() function, I can't get it to work the way I want it to...

"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Hai Nguyen_1
Honored Contributor

Re: awk stumper

What is supposted to be substituted in your output line?

Hai
Steven Mertens
Trusted Contributor
Solution

Re: awk stumper

next try :)

awk '{FS=":";sub("u_maxtries","0"); print'} root

Steven
Craig Rants
Honored Contributor

Re: awk stumper

Steven, don't know what I was fat fingering, but here's what i did to sub for the entire field.

awk '{FS=":";sub("u_maxtries#[0-9]*","u_maxtries#6");print'} root

Thanks,
Craig
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Fred Martin_1
Valued Contributor

Re: awk stumper

I wish I had time to write some code but I don't - however if you need to know the field number of a match, you could use the for loop in awk to loop thru each field looking for the match.

Basically:

for (field=1;field<=NF;field++)

If theres's a match during the looping, the variable "field" gives you the field number.
fmartin@applicatorssales.com