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
# 8  
Old 09-07-2010
Quote:
Originally Posted by agama
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]

Thanks once again .. just a one more question

Now that i got o/p like
Code:
apply_server=DBNAME apply_schema=ASN

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

Code:
apply_server=DBNAME

Please help me out

Last edited by Scott; 09-10-2010 at 01:56 PM.. Reason: Added code tags
# 9  
Old 09-07-2010
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:

Code:
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.
# 10  
Old 09-08-2010
Try:
Code:
awk '{for(i=1;i<NF;i++){if($i ~ /^apply_server/){print $i}}}' file

# 11  
Old 09-08-2010
Quote:
Originally Posted by Franklin52
Try:
Code:
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

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

Last edited by Scott; 09-10-2010 at 01:56 PM.. Reason: Added code tags
# 12  
Old 09-09-2010
Something like this?
Code:
awk '{for(i=1;i<=NF;i++){if($i ~ /apply_server|apply_schema/){print $i}}}' file

# 13  
Old 09-10-2010
Quote:
Originally Posted by Franklin52
Something like this?
Code:
awk '{for(i=1;i<=NF;i++){if($i ~ /apply_server|apply_schema/){print $i}}}' file

Thanks for relply
But that prints like this

Code:
apply_server=1
apply_schema=2
apply_server=3
apply_schema=4

I want o/p like
Code:
apply_server=1 apply_schema=2
apply_server=3 apply_schema=4

Could you please help me

Last edited by Scott; 09-10-2010 at 01:57 PM.. Reason: Added code tags
# 14  
Old 09-10-2010
Try this:
Code:
awk '{
  for(i=1;i<=NF;i++){
    if($i ~ /apply_server/){s=$i}
    if($i ~ /apply_schema/){print s FS $i}
  }
}
' file

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