Filesystem pattern match in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filesystem pattern match in awk
# 1  
Old 12-13-2016
Error Filesystem pattern match in awk

Hi, I'm trying to grep appln processes using its filesystem and also using awk to get accurate results, however when i'm uisng the filesystem in awk statement i'm getting error. Requesting help.

Code:
ps -eaf | grep ApplnName | awk '/ /opt/xxx/yyy / { print }'

Trying with this above code; getting error
Code:
syntax error The source line is 1.
 The error context is
                / /opt/xxx/yyy / >>>  { <<<
 awk: Quitting

also tried ps -eaf | grep ApplnName | awk '/ \/opt/xxx/yyy\/ { print }' but getting same error

Last edited by rbatte1; 12-13-2016 at 10:51 AM.. Reason: Changed one pair of ICODE & HTML tags to CODE tags.
# 2  
Old 12-13-2016
Hello sam_bd,

Could you please try following and let me know if this helps you(Not tested though).
Code:
 ps -eaf | grep ApplnName | awk '/\/opt\/xxx\/yyy/'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-13-2016
Or
Code:
ps -eaf | grep '[A]pplnName' | awk '$0~"/opt/xxx/yyy"'

Or a pure string search (no ERE)
Code:
ps -eaf | grep '[A]pplnName' | awk 'index($0,"/opt/xxx/yyy")'

{print} is the default action in awk.
The [ ] trick prevents from matching the grep argument in the ps list.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 12-13-2016
Gents, why the grep?

Code:
ps -eaf | awk '/[Aa]pplname/ && /\/opt\/xxx\/yyy/'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-18-2016
sorry for delay. Ravinder: yes, your suggested code did work; Thank you.
M.i.Germany: ps -eaf | grep '[A]pplnName' | awk 'index($0,"/opt/xxx/yyy")' worked for me. Thank you.
RudiC: your suggestion did work for me. Learnt a new awk thing regarding selecting processes. Thank you.

---------- Post updated at 05:14 PM ---------- Previous update was at 05:00 PM ----------

but one thing i'm not clear is the option -x which shows command line in extended format for the "ps" command. I saw an "-x" added to "ps" command in another script. I just added "-x" to the code suggested above by you all. And i got the exact count (11) of the processes for that ApplnName. If i didn't add "-x", i am getting the correct output but with 3 other processes missed. Not clear what difference "-x" made? I did go thru the 'man' page of 'ps' command and got to know that '-x' shows command line in extended format. But not very clear about it.
# 6  
Old 12-18-2016
In fact, this is NOT an "awk thing regarding selecting processes" - it's one of the strengthes of awk to match regexes found in the input (text) stream. ps output IS text, and it happens to contain the "applname" word (or not).

Depending on the system that you work on - which you fail to mention, BTW - ps comes in various flavours. Guessing from the options you use in post#1, you're on a linux system? From its man ps:
Quote:
This version of ps accepts several kinds of options:

1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
3 GNU long options, which are preceded by two dashes.
Options of different types may be freely mixed, but conflicts can appear.
-x is one of them - read carefully what it really does for you. Me personally, I don't think its needed (in non-BSD systems).
# 7  
Old 12-18-2016
I'm on HP-UX B.11.23. Amazed to see the function of awk in this case & realized output of ps command is also a data in tabular format which awk deals with. Need to dig deeper in awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

2. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

3. Shell Programming and Scripting

awk Pattern Match One File to Another

I want to read from file 1 and pattern match in file two and print field two from the next line. File 1: user1 user2 user3 File 2: name=user1 gud=12345 name=user2 gud=32456 I have this pattern hardcoded but can't work out how to pass file 1 to the pattern match: (6 Replies)
Discussion started by: u20sr
6 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

6. Shell Programming and Scripting

pattern match .com in awk script

guys ! I want to search .com,.html files ..... how do I match pattern...? here's wht I hv written if ( $i ~ /.com/ ) even escaping it doesn't help if ( $i ~ /\.com/ ) (2 Replies)
Discussion started by: shreeprabha
2 Replies

7. UNIX for Dummies Questions & Answers

awk -repeated pattern match

Hi. How can I write this differently: awk '$3 ~ /0001/{print}' Is there a way to write 0001 differently. I am looking for the pattern 01, with 3 or more 0 and 3 or more 1 in a pattern. Thanks. (12 Replies)
Discussion started by: danieladna
12 Replies

8. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

9. Shell Programming and Scripting

want to pattern match using awk

Hello Friends, My script gives an output like below:- but i only want the red part to be displayed. how to i do that. I am enclosing my shell script after that. id='CCRCWebServerINSTALLDIR' id='AdministrationTools-CINSTALLDIR' id='AdministrationTools-ent-CINSTALLDIR'... (3 Replies)
Discussion started by: asirohi
3 Replies

10. Shell Programming and Scripting

how do i pattern match a field with awk?

hi, let's say $numbers = "324 350 587" an so on... what i'm trying to do is this: awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file # file looks like this: 214 ..... 215 ... 216 .... 250 ... 324 325 ... 350 something ... ... 587 ... (4 Replies)
Discussion started by: someone123
4 Replies
Login or Register to Ask a Question