LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-29-2010, 05:23 AM   #1
saurabhmehan
Member
 
Registered: Jul 2010
Posts: 44

Rep: Reputation: 0
Unhappy 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
 
Old 07-29-2010, 08:36 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.

Last edited by David the H.; 07-29-2010 at 08:41 AM. Reason: updated
 
0 members found this post helpful.
Old 07-29-2010, 08:55 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
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
 
Old 07-29-2010, 09:51 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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"
 
Old 07-29-2010, 11:04 AM   #5
saurabhmehan
Member
 
Registered: Jul 2010
Posts: 44

Original Poster
Rep: Reputation: 0
Question

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. View Post
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.
 
  


Reply

Tags
asap


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dispaly Problem with RHEL 5.0 suri.r1984 Linux - Newbie 1 03-10-2010 08:27 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM
problem exporting result of awk to an array... mitramcc Linux - Newbie 2 02-10-2009 11:47 AM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration