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
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.
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
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.
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
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