LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem in dispaly of output using awk in array iteration. (https://www.linuxquestions.org/questions/linux-newbie-8/problem-in-dispaly-of-output-using-awk-in-array-iteration-822817/)

saurabhmehan 07-29-2010 05:23 AM

Problem in dispaly of output using awk in array iteration.
 
My script is not diaplying output as it contains awk while iterating over the array.
My script is as follows:

shortcodes=( "56882" "58585" "58888" "57575" "57677" );
for shortcode in ${shortcodes[@]}
do
echo "`awk -F\"|\" '/ShortCode=tel:$shortcode/ { arr[substr(\$2,1,4)]++ } END { for( no in arr) { print no , arr[no] } }' App_OP.log.2010-07-11`"
done


Please help me

David the H. 07-29-2010 08:36 AM

First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Second, it would help to give all the relevant information you can. What do the contents of App_OP.log.2010-07-11 look like? Are you getting any errors or other output? Is this the entire script, or is it part of a larger one?

In any case, some problems and comments I see:

The big one is that $shortcode is a bash variable inside an awk statement, and the awk statement is protected by single-quotes, so it's never expanded. You need to import the bash variable into an awk variable, usually by using the -v option, although you can usually "unprotect" them through creative quoting instead.

Is the field separator really "|", including quotes? Because the backslashes protect the quotation marks from the shell so that they become literal parts of the awk variable string.

Finally, $(..) is recommended over `..`. But actually, you shouldn't need either one, because awk prints to stdout anyway, making echo redundant.

grail 07-29-2010 08:55 AM

Of course with so little information, as pointed out by David, this is pure speculation, but the following might work:
Code:

awk 'BEGIN{FS="|";split("56882 58585 58888 57575 57677",arr," ")}/Shortcode=tel:/{for(x in arr)if($0 ~ x)out[substr($2,1,4)]++}END { for( no in arr) { print no , arr[no] }' App_OP.log.2010-07-11

ghostdog74 07-29-2010 09:51 AM

Code:

#!/bin/bash

while read -r line
do
  case "$line" in
  *ShortCode=tel* )
      l="${line##*ShortCode=tel:}"
      code="${l%% *}"
      case "$code" in
        56882|58585|58888|57575|57677 )
            IFS="|"
            set -- $line
            echo "${2:0:4}"
      esac
  esac
done <"file"


saurabhmehan 07-29-2010 11:04 AM

1. David apologies for inconvenience caused.
2. Contents in the file are pipe(|) separated and i want to grep only those records which have tel:56882 tel:58585 tel:58888 tel:57575 tel:57677 in their lines one by one and after that i need first four characters of second field in the line and their respective count in the resulted logs against each grep.
For example:
Contents of file -
2010-07-29 13:45:32.334|9818250197|http://xyz.com?shortcode=tel:58888&keyword=test
2010-07-29 13:45:32.334|9871250197|http://xyz.com?shortcode=tel:58888&keyword=test
2010-07-29 13:45:32.334|9871250197|http://xyz.com?shortcode=tel:58888&keyword=test
2010-07-29 13:45:32.334|9818250198|http://xyz.com?shortcode=tel:57677&keyword=test
2010-07-29 13:45:32.334|9818250198|http://xyz.com?shortcode=tel:57677&keyword=test
2010-07-29 13:45:32.334|9876250198|http://xyz.com?shortcode=tel:57677&keyword=test
2010-07-29 13:45:32.334|9876250198|http://xyz.com?shortcode=tel:57677&keyword=test
2010-07-29 13:45:32.334|9818250196|http://xyz.com?shortcode=tel:57575&keyword=test
2010-07-29 13:45:32.334|9818250196|http://xyz.com?shortcode=tel:57575&keyword=test

Output will be
For Shortcode : 58888

9818 = 1
9871 = 2

For Shortcode : 57677

9818 = 2
9876 = 2

For Shortcode : 57575
9818 = 2

and after that map the series with the region for instance 9818,9876 lies in North and 9871 lies in south with other array that is split(9818 9876,North," ") and split(9871 9875,SOUTH," ") and get the sum region wise. So the final output will be
For Shortcode : 58888
North = 1
South = 2

For Shortcode : 57677
North = 4
South = 0

For Shortcode : 57575
North = 1
South = 0

Hope you got my query.
3. No i am not getting any error while executing but blank output and this is the entire script.





Quote:

Originally Posted by David the H. (Post 4049060)
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Second, it would help to give all the relevant information you can. What do the contents of App_OP.log.2010-07-11 look like? Are you getting any errors or other output? Is this the entire script, or is it part of a larger one?

In any case, some problems and comments I see:

The big one is that $shortcode is a bash variable inside an awk statement, and the awk statement is protected by single-quotes, so it's never expanded. You need to import the bash variable into an awk variable, usually by using the -v option, although you can usually "unprotect" them through creative quoting instead.

Is the field separator really "|", including quotes? Because the backslashes protect the quotation marks from the shell so that they become literal parts of the awk variable string.

Finally, $(..) is recommended over `..`. But actually, you shouldn't need either one, because awk prints to stdout anyway, making echo redundant.



All times are GMT -5. The time now is 05:25 PM.