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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question on awk for finding the column number using a match word
# 1  
Old 09-05-2010
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

Code:
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
Code:
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

Last edited by Scott; 09-08-2010 at 07:12 PM.. Reason: Please use code tags
# 2  
Old 09-05-2010
Using the syntax $(i) will allow you to access a column using the contents of a variable, in this case 'i'.

Code:
#!/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.
This User Gave Thanks to agama For This Post:
# 3  
Old 09-05-2010
Code:
awk '{gsub(/_/,"",$5);gsub(/_/,"",$6);print $1,$5,$6,"stop"}' infile

or something like:


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

# 4  
Old 09-05-2010
Quote:
Originally Posted by agama
Using the syntax $(i) will allow you to access a column using the contents of a variable, in this case 'i'.

Code:
#!/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.
Thanks for the quick reply

Code:
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 ----------

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

Code:
#!/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.
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..

Last edited by Scott; 09-10-2010 at 01:55 PM.. Reason: Added code tags
# 5  
Old 09-05-2010
Sorry, misinterpreted your need. Maybe this sample will give you what you need:

Code:
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.
This User Gave Thanks to agama For This Post:
# 6  
Old 09-05-2010
Thanks a lot

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

Code:
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

Last edited by Scott; 09-10-2010 at 01:55 PM.. Reason: Added code tags
# 7  
Old 09-06-2010
Quote:
Originally Posted by mallak
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_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:

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

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Finding a word with awk or sed

Hello, in a AIX system : AIX CDRATE01 2 7 00FAB3114C00 my following commande give the result : LISTE /tmp/RESS **************************************************************** Liste TYPE = XXXXXXX EX = YYYY VER ... (13 Replies)
Discussion started by: sam01
13 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

awk match whole word using a variable

Hello, I am writing a script in awk where I need to match a whole word that is stored inside a variable. For example: I am working on a text that looks like this, and I want to print the second row: sfasfsomethingsfasf this is something I can use this is not somethingIcanuse ... (12 Replies)
Discussion started by: avi.levi
12 Replies

5. Shell Programming and Scripting

Finding log files that match number pattern

I have logs files which are generated each day depending on how many processes are running. Some days it could spin up 30 processes. Other days it could spin up 50. The log files all have the same pattern with the number being the different factor. e.g. LOG_FILE_1.log LOG_FILE_2.log etc etc ... (2 Replies)
Discussion started by: atelford
2 Replies

6. Shell Programming and Scripting

finding number in exact column

Dear all, I want to find a number in exact column but I don't know how to do it. Here is the thing, data is shown below, and I want to find 416 in the first column and print it out, how should I deal with it? Thank you very much! ab33 50S01S 958 279.068999 67.251013 -150.172544 67.250000... (5 Replies)
Discussion started by: handsonzhao
5 Replies

7. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

8. UNIX for Dummies Questions & Answers

AWK lookup not finding match

Hello everyone, I have been struggling with the following situation, I think I am doing something wrong, can anyone help? I have 2 comma separated files, the first is a look-up table that will supply the phone number based on the customer id, the second is a file containing customers and their... (4 Replies)
Discussion started by: gio001
4 Replies

9. Shell Programming and Scripting

Finding multiple column values and match in a fixed length file

Hi, I have a fixed length file where I need to verify the values of 3 different fields, where each field will have a different value. How can I do that in a single step. (6 Replies)
Discussion started by: naveen_sangam
6 Replies

10. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies
Login or Register to Ask a Question