Question on awk for finding the column number using a match word

Hi Guys,

Please help me out in my situation of writing a shell script

Exampl:I have a output like

asnapply 1 2 3 apply_server=1 apply_schema=ASN
asnapply 1 2 3 apply_server=2 apply_schema=ASN

Now i need output like

asnacmd applysever=1 applyschema=ASN stop
asnacmd applysever=2 applyschema=ASN stop

I tried awk but hardcoded and printed but i have a question instead of hardcoding the column number,how can i get the column number matching apply_server from input and print apply_server=1

Any help appreciated
Thanks

Using the syntax $(i) will allow you to access a column using the contents of a variable, in this case 'i'.

#!/usr/bin/env ksh
awk -v col=${1:-1} ' {print $(col)}'

This example accepts a column number from the command line and prints just that column from stdin. If no column number is given, it defaults to the first column.

1 Like
awk '{gsub(/_/,"",$5);gsub(/_/,"",$6);print $1,$5,$6,"stop"}' infile

or something like:

awk '{$5="applysever=" NR;gsub(/_/,"",$6);print $1,$5,$6,"stop"}' infile

Thanks for the quick reply

awk '{$5="applysever=" NR;gsub(/_/,"",$6);print $1,$5,$6,"stop"}' infile

That was the problem for Me i cannot say $5 to applyserver, becoz it can be any where.

My awk command shuld search where applyserver is and return me the value of the column

---------- Post updated at 09:32 PM ---------- Previous update was at 09:28 PM ----------

Thanks for response

My Requirement is that AWK program should search "applyserver" string in the line's and return me the column number.so that i can use the column number to use and print that column number

I cannot pass the value from command line..

Sorry, misinterpreted your need. Maybe this sample will give you what you need:

awk '
        {
                if( match( $0, "apply_server=.*[ \t]" ) )    # search the input line for the pattern
                        print substr( $0, RSTART, RLENGTH );  
        }
'

If the match function finds apply_server= followed by any characters followed by a space or tab, it prints that part of the input line. On a successful match, RSTART is set to the index of the pattern matched and RLENGTH is set to the length of the string.

If you need to capture the string and use it, rather than printing it, you could assign the substring to a variable.

Hope this is more of what you needed.

1 Like

Thanks a lot

So i understood that using above code awk will scan each line and will return values 1 and 2

asnapply 1 2 3 apply_server=1 apply_schema=ASN
asnapply 1 2 3 apply_server=2 apply_schema=ASN

Thanks once again for the reply

No, it will find the whole string apply_server=1; If you need it to find just the value after the equal, use this:

split( substr( $0, RSTART, RLENGTH ), a, "=");   # split the token at the =
value = a[2];                           # everything after equal is in a[2]
1 Like

Thanks once again .. just a one more question

Now that i got o/p like

apply_server=DBNAME apply_schema=ASN

My Question is from this O/P i need to get only

apply_server=DBNAME

Please help me out

The test case that I used didn't have a problem, but I realised looking at the match() command I posted earlier that it will over match if there is more than one space or tab on the line following the pattern. Try this, slight change to the pattern in the match:

awk '
        {
                if( match( $0, "apply_server=[^ \t]*[ \t]*" ) )    # search the input line for the pattern
                        print substr( $0, RSTART, RLENGTH );
        }
' <input-file

Hope this works better for you.

Try:

awk '{for(i=1;i<NF;i++){if($i ~ /^apply_server/){print $i}}}' file

Thanks franklin,

Its olved problem to one extent..a quick question

Input

asnqcap apply_server=1 capture_schema=2

I want o/p with both apply_server and capture_schema

From above code i will get only apply_server

Please help me

Something like this?

awk '{for(i=1;i<=NF;i++){if($i ~ /apply_server|apply_schema/){print $i}}}' file

Thanks for relply
But that prints like this

apply_server=1
apply_schema=2
apply_server=3
apply_schema=4

I want o/p like

apply_server=1 apply_schema=2
apply_server=3 apply_schema=4

Could you please help me

Try this:

awk '{
  for(i=1;i<=NF;i++){
    if($i ~ /apply_server/){s=$i}
    if($i ~ /apply_schema/){print s FS $i}
  }
}
' file

Thanks for your patience franklin,

One more question:The above code is checking both apply_server and apply_schema..My need is that o/p shuld come if either one is existing..
My input can have apply_server or apply_schema or both.. I need o/p like that only..Please help me in this reg

Something like this?

awk '{
  for(i=1;i<=NF;i++){
    if($i ~ /apply_server/ || $i ~ /apply_schema/){
      if(s){
        s=s FS $i
      } else {
        s=$i
      }
    }
  }
  print s; s=""
}
' file
1 Like

Thanks franklin..suites for my requirements..Thanks once again for your kind help