New at scripting awk with variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New at scripting awk with variable
# 1  
Old 12-11-2015
New at scripting awk with variable

I am trying to write a shell script that will add a date to and awk command from the command prompt.

here is the line I am having difficulties.

Code:
zgrep -i playback $dir/$1-*-errors.out.gz  |cut -d '|' -f 1,11,12,15 | awk -v start=${start} -v end=${end} -F '|' '{$1>=start  && $1 <=end} {print $2 ,$3 ,$4}' | sort -n  |uniq

The logs are in epoc so I added this to the script


Code:
start=`date --date="$2" +%s000`
end=`date --date="$3" +%s000`

to execute the command...
Code:
#Get_Error.sh  20151201 "20151201 05:11:00" "20151201 07:50:00"

Not sure how to add the variable to the awk command without it braking. If I run it manually with epoc timestamps it runs fine.



Any help would be appreciated.

Thanks

Last edited by Don Cragun; 12-11-2015 at 06:41 PM.. Reason: Add CODE tags.
# 2  
Old 12-11-2015
Have each $var in quotes!
For example
Code:
awk -v "start=${start}" ...

Or
Code:
awk -v start="${start}" ...

# 3  
Old 12-11-2015
Quote:
Originally Posted by infinity0880
I am trying to write a shell script that will add a date to and awk command from the command prompt.

here is the line I am having difficulties.

Code:
zgrep -i playback $dir/$1-*-errors.out.gz  |cut -d '|' -f 1,11,12,15 | awk -v start=${start} -v end=${end} -F '|' '{$1>=start  && $1 <=end} {print $2 ,$3 ,$4}' | sort -n  |uniq

The logs are in epoc so I added this to the script


Code:
start=`date --date="$2" +%s000`
end=`date --date="$3" +%s000`

to execute the command...
Code:
#Get_Error.sh  20151201 "20151201 05:11:00" "20151201 07:50:00"

Not sure how to add the variable to the awk command without it braking. If I run it manually with epoc timestamps it runs fine.



Any help would be appreciated.

Thanks
I don't understand what problem you're having.

Are you saying that if you change Get_Error.sh to:
Code:
start=`date --date="$2" +%s000`
end=`date --date="$3" +%s000`
zgrep -i playback $dir/$1-*-errors.out.gz |
    cut -d '|' -f 1,11,12,15 |
    awk -v start=${start} -v end=${end} -F '|' '{$1>=start  && $1 <=end} {print $2 ,$3 ,$4}' |
    sort -n | uniq

or:
Code:
zgrep -i playback $dir/$1-*-errors.out.gz |
    cut -d '|' -f 1,11,12,15 |
    awk -v start=`date --date="$2" +%s000` -v end=`date --date="$3" +%s000` \
        -F '|' '{$1>=start  && $1 <=end} {print $2 ,$3 ,$4}' |
    sort -n | uniq

it doesn't work when you run the command you showed us at a root user shell prompt?

What operating system are you using?

What shell is being used to run Get_Error.sh?

What output do you get from the command:
Code:
date --date="20151201 05:11:00" +%s000

on your system?

Note to MadeInGermany: If $start and $end are set to values that are presented as Seconds since the Epoch values followed by three zeros, the quotes shouldn't matter
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 12-12-2015
What does "it braking" mean? Do you get error messages? Please post! Do you just get nothing? Pls say so...
What does "run it manually with epoc timestamps" mean? Running the script or running the awk command?
# 5  
Old 12-18-2015
Thanks for all the feedback guys. I've been away on training for a while. I will try all the different ideas and will post the results.

Cheers,

---------- Post updated at 01:25 PM ---------- Previous update was at 10:16 AM ----------

Everything works in the bash script except that I get the error count and not the errors, I would like to have the errors.
The command is entered with human readable time and the script converts it to epoc because the logs are in epoc.


this is what I need to have from prompt.
GetUserImpact.sh 20151201 "20151201 05:11:00" "20151201 07:50:00"

Code:
[zgrep -i playback $dir/$1-*-errors.out.gz  | cut -d '|' -f 1,11,12,15 | gawk -v 'start="${start}" -v end="${end}"' -F '|' '$1>= $start && $1 <= $end {print $2 ,$3 ,$4}'  | sort -n | uniq -c]


Last edited by infinity0880; 12-18-2015 at 02:35 PM..
# 6  
Old 12-18-2015
From what you have shown us, how are we supposed to guess where the errors are hidden in you input? You haven't shown us how your input is formatted.

When we look into our crystal balls to see what you have on your screen in front of you, we are not seeing much. Are you wearing dark glasses?

Please show us a sample of the data you're trying to process (in CODE tags)!
# 7  
Old 12-18-2015
Good point, too many hours with no coffee...and Like I said I'm new at writing scripts.

File ex
Code:
1449332076709|video_load_time|5.1.1|SM-N910W8|5.1.1|1193.0|CELLULAR|AUTO_AUTHENTICATED|56.2577877|-118.6077102|0000000000000000000000000000-023X_wap2.mobility.ca|||MediaChannel501|N/A||11608

the last field being the error the 0000000000000000000000000000-023X_wap2.mobility.ca field is an identifier.


Or is there a way that I can write something (not using AWK) that will look in a file from start time to end time. I could send that to a file then compile the data from there. Then I won't need to fix AWK

This way the user can enter
Ex.
Code:
 GetUserImpact.sh  20151205 "20151205 00:11:00" "20151205 23:50:00"

Moderator's Comments:
Mod Comment Please use CODE tags for full-line and multi-line sample input, output, and code segments (not ICODE tags) and use ICODE tags (not double quotes) for shorter input and output samples.

Last edited by Don Cragun; 12-18-2015 at 06:19 PM.. Reason: Change ICODE tags to CODE tags and add CODE tags.
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 assign a value to a variable in awk scripting?

Hi, I am trying to assign a value using below command and it is assigning the command to the variable not the output of the command? out_value="echo $0 | cut -c 9-11"; How can i assign the output to the variable instead of whole command? This is inside my awk script (7 Replies)
Discussion started by: bhagya123
7 Replies

2. Shell Programming and Scripting

Need help in replacing a variable in shell scripting

Experts, Can someone help me with the below? I've a variable called NSPMHOME=some_value. I would like change the value of the variable with another variable and something followed by it. E.g From NSPHOME=some_value to NSPMHOME=$SOMETHING/test $SOMETHING has value /opt/tag . The end... (4 Replies)
Discussion started by: jawaugh
4 Replies

3. Shell Programming and Scripting

Passing variable as a file-Scripting Help

Hi Guys, i have a file where data is in the below format:: data1 data2 data3 data4 data4 data6 my script written as:: #!/bin/ksh cd $1 at now <<END sh $2 END Here i want to pass the values stored in the above file one by one till the end of line. Here if i am doing it as:: (2 Replies)
Discussion started by: Atp3530
2 Replies

4. Shell Programming and Scripting

Scripting to Duplicate Lines Based on Variable

Greeting all! I could use some assistance please. :) I've been searching for the best way to duplicate a line based on a variable in the next line. Sample Data: Nov 22 00:00:19 10.10.10.1 "%ASA-4-313005: No matching connection for ICMP error message: icmp src Outside:1.2.3.4 dst... (3 Replies)
Discussion started by: sjrupp
3 Replies

5. Shell Programming and Scripting

Shell scripting Variable doubt

Dear Friends, If i give the command echo $- i am getting output of himBH. Can some body explain what does it means echo $- himBH Thanks in Advance Rajkumar (2 Replies)
Discussion started by: rajkumarin
2 Replies

6. Shell Programming and Scripting

Use case insensitive variable in ksh shell scripting using sed or awk

I am using a variable called $variable in a pattern search to print from a starting variable to a constant value. the variable search should be case in sensitive. i tired using Ip at the end in the below command. but in ksh it is not working. sed -n "/$variable/,/constant/p" file i also... (11 Replies)
Discussion started by: johnjs
11 Replies

7. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

8. Shell Programming and Scripting

Help in scripting, store value in variable

Hi all, I have a script in which i need to run a command like "/opt/dell/srvadmin/sbin/omreport about" and output will be something like Version : 6.3.0 Copyright : Copyright (C) xxx Inc. 1995-2010 All rights reserved. Company : xxx Inc. In this i need to save the version... (13 Replies)
Discussion started by: Renjesh
13 Replies

9. Shell Programming and Scripting

Bash scripting, question about a variable

Hey, So I've run into a problem, due to my limited knowledge of Bash scripting. Basically I've got a long script and I want to understand it before I even try and edit it. As long as I don't understand the script, I will not bother editing it. Anyway, the following variable confuses me... (5 Replies)
Discussion started by: abciscool
5 Replies

10. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies
Login or Register to Ask a Question