awk search with begin


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk search with begin
# 8  
Old 03-24-2014
That is not how it works.
As Don mentioned, BEGIN statements are primarily used for initialization or things of that sort. It gets called before the file is parsed.

What is your actual purpose?
# 9  
Old 03-24-2014
I have input file like below

Code:
 aker1 n Wed Jan 15 2014 23:29 on
89jvh
89jvh n Fri Mar 21 2014 10:17 on
456fh 
456fh  n Sat Mar 22 2014 14:59 on
r456f
r456f n Mon Mar 24 2014 18:23 on
o8hij
o8hij n Thu Mar 13 2014 22:26 on
t45fe Mar 24 2014 10:07:01 on pt
t45fe
t45fe Mar 24 2014 10:07:18 on pt
45drv
45drv Mar 24 2014 10:07:18 on pt
45drv
67mug Mar 24 2014 10:07:19 on pt
t67mug
r56fz Mar 24 2014 10:07:19 on pt
r56fz
r56fz Mar 24 2014 10:07:20 on pt

if the second column does not have value 'n' then the script should execute.

I tried below script. but it is throwing error.

Code:
#!/bin/ksh
/usr/xpg4/bin/awk '
BEGIN {X["Jan"] = "01"; X["Feb"] = "02"; X["Mar"] = "03"; X["Apr"] = "04";
       X["May"] = "05"; X["Jun"] = "06"; X["Jul"] = "07"; X["Aug"] = "08";
       X["Sep"] = "09"; X["Oct"] = "10"; X["Nov"] = "11"; X["Dec"] = "12"; 
      };
if($2 != 'n') && NR > 1 {print $1 "\t" $3 "," X[$2] "," $4 " " $5}' inputfile.txt | sed "s/,, //g" | sed "s/,/./g"|sed "s/on//g" >> ouputfile.txt

Code:
error
/usr/xpg4/bin/awk: file "[command line]": line 5: syntax error  Context is:
>>>     "; X["Nov"] = "11"; X["Dec"] = "12"; 
>>>           };
>>>     if      <<<
+ 1>> userreport1.txt

# 10  
Old 03-25-2014
Try changing:
Code:
        ...
      };
if($2 != 'n') && NR > 1 { ...

to:
Code:
        ...
      }
$2 != "n" && NR > 1 { ...

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Usage of a variable in awk BEGIN

Hi, diffcount=`awk 'BEGIN { while ( getline < "/scripts/matt/text.server1.reference" ) { arr++ } } { if (!( $0 in arr ) ) { print } }' $TMPDIR/$(basename $0 .sh) | wc -l` if ]; then OK="OK - No change in the interfaces status" elif ]; then DIFF=`awk 'BEGIN {... (4 Replies)
Discussion started by: nms
4 Replies

3. UNIX for Beginners Questions & Answers

Awk: use variable defined in begin

Hi there, I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so. NF/12 gives me on how many iterations I've to do to check certain value. For example: 7, 14th if only 24 columns in file. 7th, 14th and 21st if 36 columns in... (6 Replies)
Discussion started by: genome
6 Replies

4. Shell Programming and Scripting

Awk: BEGIN: prints nothing

My code fails to do anything if I've BEGIN block in it: Run the awk script as: awk -f ~/bin/sum_dupli_gene.awk make_gene_probe.txt #!/usr/bin/awk -f BEGIN { print ARGV #--loads of stuff } END{ #more stuff } (14 Replies)
Discussion started by: genome
14 Replies

5. Shell Programming and Scripting

Search ad replace using begin and end of the file

Hello Friends , Please help to create script for compare and replace if not matches of set of lines . * Primary* Servername Server1 Location R201 Rack 4 *End Primary* *Secondary* Server Name Server1 IPAddress 10.24.30.10 Application Apache *End of Secondary* Above... (4 Replies)
Discussion started by: rnary
4 Replies

6. Shell Programming and Scripting

BEGIN and END format in awk

I'm new to awk, trying to understand the basics. I'm trying to reset the counter everytime the program gets a new file to check. I figured in the BEGIN part it would work, but it doesn't. #!/bin/awk -f BEGIN {counter=0} { sum=0 for ( i=1; i<=NF;... (1 Reply)
Discussion started by: guitarist684
1 Replies

7. Programming

search a file between two begin and end strings in c

Can any one help me out with following problem... I want to search in a file which has two strings repeat each time(like start and end) i want to search between these two string in C programming. please help me with the solution. thanks in advance. (8 Replies)
Discussion started by: uday.sena.m
8 Replies

8. Shell Programming and Scripting

awk BEGIN problem

awk 'BEGIN { print "line one\nline two\nline three" }' After ./awktest.sh Usage: awk -f progfile file ... Usage: awk 'program' file ... POSIX options: GNU long options: -f progfile --file=progfile -F fs --field-separator=fs -v var=val ... (7 Replies)
Discussion started by: cola
7 Replies

9. Shell Programming and Scripting

awk getting stuck after BEGIN

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (1 Reply)
Discussion started by: akshaykr2
1 Replies

10. Shell Programming and Scripting

AWK not processing BEGIN directive

Hello I have the following awk script: BEGIN { {FS = " " } {print "\t\tIllegal Loggon Attempts on MAIL\n"} {"date" | getline d} {printf "\t %s\n",d } {print "Loggon Name\t\t\t Number of Attempts\n"} ... (2 Replies)
Discussion started by: mojoman
2 Replies
Login or Register to Ask a Question