Using "awk" with other variables from the same script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using "awk" with other variables from the same script
# 1  
Old 03-07-2012
Using "awk" with other variables from the same script

Hello all,

I'm just start using "awk" for my log processing script, actually I just use awk for grep things:

Code:
awk '/projectA/ {print;}' /usr/local/tomcat/log/server_access_log.txt > /home/lunaticdawn/log/projectA/process.log

but now I have to put it into a script so that I can add more project and add to cronjob, the original script is:
log_process.sh

Code:
# !bin/sh
# usage: log_process.sh <project name>
path=/usr/local/tomcat/log/
filename=server_access_log.txt
destpath=/home/lunaticdawn/log/

awk '/$1/ {print;}' $path$filename > $destpath$1/process.log

I searched around in internet know that need to add -v for awk, so i tried:

Code:
# !bin/sh
# usage: log_process.sh <project name>
path=/usr/local/tomcat/log/
filename=server_access_log.txt
destpath=/home/lunaticdawn/log/

awk -v VAR1=$path VAR2=$filename VAR3=$destpath '/$1/ {print;}' $VAR1$VAR2 > $VAR3$1/process.log

but error appeared: awk: VAR2=server_access_log.txt syntax error

Actually I got another errors beside this syntax error but I forgot to mark them down, I think the method I use to passing variables to awk is wrong, would you please help on advice me to use awk in a shell script, thanks a lot.

p.s. Although using grep can do the same thing, but I really want to try awk, thank you so much.
# 2  
Old 03-07-2012
Something like this?
Code:
#!/bin/sh
# usage: log_process.sh <project name>
path=/usr/local/tomcat/log/
filename=server_access_log.txt
destpath=/home/lunaticdawn/log/

awk -v Var1="$1" '$0 ~ Var1' $path$filename > $destpath${1}/process.log


Or maybe faster with grep:

Code:
grep $1 $path$filename > $destpath${1}/process.log

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 03-09-2012
Thanks Franklin,

It really solved my problem, I noticed:

Code:
awk -v Var1="$1" '$0 ~ Var1' $path$filename > $destpath${1}/process.log

I'm not so understand the
Code:
'$0 ~ Var1'

what is $0 for? Is it contain the script name?

Thanks.
# 4  
Old 03-09-2012
Quote:
Originally Posted by lunaticdawn
I'm not so understand the
Code:
'$0 ~ Var1'

what is $0 for?
$0 in awk refers to the line being read currently.

'~' is the match operator. $0 ~ Var1 is a check to see whether $0 (i.e., the current line being read) contains the value in variable Var1.
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 03-10-2012
Using weak quoting we could do something like this:
Code:
awk "/$1/" "$path$filename" > "$destpath$1/process.log"

If you do not want to use grep.

Last edited by Scrutinizer; 03-10-2012 at 06:31 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-20-2012
Thanks for the help guys, I think using "awk" to do the things that "grep" is a little bit stupid but I wanna try to learn more about "awk"

Now I'm gonna upgrade it to do more complicated task, my log is in /usr/local/tomcat/log/log.txt, I want to find different browsers from the log and put them in different files, e.g. find IE from the log and put IE's log in IE.txt by using awk.

I watched some tutorial, and know that I can use BEGIN & END on this task, so I tried:

Code:
awk -f browser.awk log.txt

browser.awk contains:
Code:
/Mozilla/ {print $0} > /home/lunaticdawn/log/mozilla.txt
/IE/ {{print $0} > /home/lunaticdawn/log/IE.txt
/chrome/{print $0} > /home/lunaticdawn/log/chrome.txt
END

i think it should work... but it doesn't, it gives syntax error, and I'm seeking for help again Smilie... thanks so much
# 7  
Old 03-20-2012
Hi, there are two {{ on line 2... Leave out the END statement...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Expect Script - Variables are Empty immediately after "Setting" Them?

Hello All, I have embedded some expect code inside a Bash script I'm writing, but for some reason any variable I 'set' to something is showing as empty immediately on the next line... I haven't run into this problem before so I'm not sure what it could be...? I'm guessing it has something to... (4 Replies)
Discussion started by: mrm5102
4 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

6. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Advanced & Expert Users

variables don't work for inline "su" script

My first post here... I cannot get variables to work when using inline KSH commands on a "su" command. I have a KSH script, below, that produces my problem: #!/usr/bin/ksh su <user_id> <<-END export FLD1=`echo $PWD` pwd echo $FLD1 echo TEST echo $PWD END The script will prompt me... (3 Replies)
Discussion started by: joekreif
3 Replies
Login or Register to Ask a Question