grep in Awk

Hi
Can i use grep statements in awk

Like the below.

cat /HS_Data_00/tmp/test/name_88_final|awk '{ printf "%s %-50s %s\n",substr($0,1,5),substr($0,12,31), grep -c "string" filename '|sort

This is a requirement. Can you please help?

Regards
Dhana

Hi All
Can any body help me out.

Regards
Dhana

Bumping your posts is against the rules, and expecting a reply within half an hour is not realistic, especially if your problem is not described in much detail.

awk by nature does a superset of what grep does. Perhaps the following somehow manages to do what you intended it to.

cat 
awk '/string/ { ++count}
{ printf "%s %-50s %i\n",substr($0,1,5),substr($0,12,31), count }' /HS_Data_00/tmp/test/name_88_final |sort

It's not clear where the filename is supposed to come from; this prints the count of matches on "string" so far in the input. If you need to manipulate two different files, then maybe it's better to separate the two after all. Where is the filename supposed to come from?

Hi
Sorry about not giving in detail.

here is the actual command that i tried.

cat /HS_Data_00/tmp/test/name_88_final|awk '{ printf "%s %-50s %s \n",substr($0,1,5),substr($0,12,31), `grep -c substr($0,1,11) /HS_Data_00/Dart/Out/sys19/lps/100109.U` }'|sort

Please refer the below command.
grep -c substr($0,1,11) /HS_Data_00/Dart/Out/sys19/lps/100109.U

This file /HS_Data_00/Dart/Out/sys19/lps/100109.U
is a separate file and i need to count the occurences of the substr($0,1,11) in this /HS_Data_00/Dart/Out/sys19/lps/100109.U
Hence i am trying to use grep command.

Please let me know if you need more info.

Regards
Dhana

You've been given 2 infractions in a single day for 'bumping up' posts.
Pls reread the Rules - they're here to be adhered to!

So you want to count the number of occurrences of substr($0, 1, 11) from the current input line from another file? Your basic approach is not too bad but it's a pretty advanced topic.

awk '{ ("grep -c " substr($0,1,11) " /HS_Data_00/Dart/Out/sys19/lps/100109.U") | getline u;
  printf "%s %-50s %s \n", substr($0,1,5),substr($0,12,31), u }' /HS_Data_00/tmp/test/name_88_final | sort

The awk getline function allows you to retrieve the output from an external command sort of like backticks in the shell.

Hi
I tried the solution that you have provided. It works in certain cases and it does'nt work when i process this file

file name = name_6_final

$ cat name_6_final
TYPE 001506GROUND 0704
TYPE 002058MICROWAVE 7285
TYPE 002694REGULAR 7285
TYPE 003584WHOLE 0704
TYPE 018389DRUMSTICK 0704
TYPE 030585GIBLET 0704
TYPE 030586BREAST-BONELESS 0704
TYPE 030587BREAST-SPLIT 0704
TYPE 030588BREAST-TENDERLOIN 0704
TYPE 030589BREAST-WHOLE 0704
TYPE 030590THIGH 0704
TYPE 030591THIGH-BONELESS 0704
TYPE 030592WHOLE LEG 0704
TYPE 030593WING 0704
TYPE 030594OTHER TURKEY 0704
TYPE 037021GROUND TURKEY PATTIES 0704
TYPE 037022CUT UP MIXED PARTS 0704
TYPE 037076OTHER HAM 0738
TYPE 037080HALF-PORTION 0738
TYPE 037081HAM STEAKS-CENTER CUT 0738
TYPE 037082WHOLE HAM 0738

When i debugged as i tried the below
$ awk '{ ("grep -c "substr($0,1,11)" /HS_Data_00/Dart/Out/sys19/lps/100109.U") | getline u }' name_6_final
grep: can't open 001506
grep: can't open 002058
grep: can't open 002694
grep: can't open 003584
grep: can't open 018389
grep: can't open 030585
grep: can't open 030586
grep: can't open 030587
grep: can't open 030588
grep: can't open 030589
grep: can't open 030590
grep: can't open 030591
grep: can't open 030592
grep: can't open 030593
grep: can't open 030594
grep: can't open 037021
grep: can't open 037022
grep: can't open 037076
grep: can't open 037080
grep: can't open 037081
grep: can't open 037082

Got the above error.

Regards
Dhana

Hi
Any idea of how to rectify this error ?

Regards
Dhana

Hi
Can you please let me know if you have any idea about rectifying the error.

Regards
Dhana

You mean wake up from my sleep to solve your problems? I think you need to check your assumptions here.

If the substring will contain whitespace or other shell special characters, you need to add quoting to the command.

("grep -c \"" substr(whatever) "\" /path/to/file") | getline u

HI
Thank you so much for your patience and ideas.
It did work perfectly.

Regards
Dhana