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
# 8  
Old 03-21-2012
Hi, Scrutinizer

that's a typing mistake on the post sorry

I erased the "END" and it still has syntax error

Code:
awk: browser.awk:1: /Mozilla/ {print $0} > /home/lunaticdawn/log/mozilla.txt
awk: browser.awk:1: /Mozilla/ {print $0} ^ syntax error
awk: browser.awk:1: /Mozilla/ {print $0} > /home/lunaticdawn/log/mozilla.txt
awk: browser.awk:1: /Mozilla/ {print $0} > /home/lunaticdawn/log/mozilla^ sysntax error

# 9  
Old 03-21-2012
Ow yes you should put double quotes around the path/filename. In some awks you need to put it in a variable first and print to the variable...
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 03-21-2012
Thanks Scrutinizer

as your advice, solved a syntax error, but still got a syntax error on the " > "

I tried a few times, like ">>", insert { }, still cannot solve it

the print function is work, but the output to another file part drive me crazy...
# 11  
Old 03-21-2012
Try this:
Code:
awk '
  /Mozilla/{print>moz}
  /IE/     {print>ie} 
  /chrome/ {print>chr}
' moz=/home/lunaticdawn/log/mozilla.txt ie=/home/lunaticdawn/log/IE.txt chr=/home/lunaticdawn/log/chrome.txt infile

Perhaps we could make the script more universal.. Could you post part of the Tomcat log?
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 03-21-2012
Thanks Scrutinizer

/Mozilla/{print>moz}
this one exactly can do what I want!! just a simple {print>xxx}Smilie!!!

awk -f browser.awk server.log
browser.awk
Code:
moz = /home/lunaticdawn/moz,txt
/Mozilla/ {print>moz}

it works now!!! Thanks so much!!!


this is part of the log
Code:
"192.168.0.1" "NULL-AUTH-USER" "21/Mar/2012:08:30:07 +0000" "HTTP/1.1" "Mozilla/4.0" 200

at first I tried using IFS and case to do, but I dont know how to print the hold line that match the case, so I try to use awk now, thanks Scrutinizer.
# 13  
Old 03-21-2012
That's a bit short. Assuming that the browser is always in the before last column, you could try if this works:
Code:
awk -F'["/]' '{f=dir "/" $(NF-2) ".txt";print>f}' dir=/home/lunaticdawn/log infile

This would use the name of the browser from the log and write to a file with a corresponding name...
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 03-21-2012
Quote:
Originally Posted by Scrutinizer
That's a bit short. Assuming that the browser is always in the before last column, you could try if this works:
Code:
awk -F'["/]' '{f=dir "/" $(NF-2) ".txt";print>f}' dir=/home/lunaticdawn/log infile

This would use the name of the browser from the log and write to a file with a corresponding name...
wow you are an awk genius!! it work perfectly!!

may I ask how it works? NF i know its number of fields, NF-2 means last 2 field right?

it automatically create the output files its so magic, but I'm not so understand how this works
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