awk search with begin


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk search with begin
# 1  
Old 03-24-2014
awk search with begin

Hi,

I have written below script to begin if the line has n

Code:
#!/bin/ksh
/usr/xpg4/bin/awk {/ n / '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["Dev"] = "12"; };}
NR > 1 {print $1 "\t" $5 "," X[$4] "," $6 " " $7}'} input.txt | sed "s/,, //g" | sed "s/,/./g"|sed "s/on//g" >> output.txt

but getting below error when compile

Code:
/usr/xpg4/bin/awk: syntax error Context is:
>>> / n / BEGIN <<<

Code:
Input file
 
e45ds n Mon Mar 24 2014 14:53 on
34def
34def n Mon Sep 23  2013 on pts/
4mki5
4mki5
976vh n Mon Mar 24 2014 10:06 on

The purpose is to convert
Code:
 24.03.2014

like this format.

Thanks
# 2  
Old 03-24-2014
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["Dev"] = "12"; }
NR > 1 && $0 ~ /n/ {print $1 "\t" $5 "," X[$4] "," $6 " " $7}' input.txt | sed "s/,, //g" | sed "s/,/./g"|sed "s/on//g" >> output.txt


Last edited by SriniShoo; 03-24-2014 at 05:28 AM..
# 3  
Old 03-24-2014
You corrected awk statement

Code:
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";
     }
     / n / && NR>1{ print $1 "\t" $5 "," X[$4] "," $6 " " $7} ' input.txt | ...


A modified version without sed as per your input, output and code provided.
Code:
awk 'BEGIN{ X["Jan"]=1; X["Feb"]=2;  X["Mar"] = 3;  X["Apr"] = 4;
            X["May"]=5; X["Jun"]=6;  X["Jul"] = 7;  X["Aug"] = 8;
            X["Sep"]=9; X["Oct"]=10; X["Nov"] = 11; X["Dec"] =12;
     }
     / n / && NR>1{ printf("%s\t%d.%02d.%d %s\n", $1, $5, X[$4], $6, $7!~"[0-9]"?"":$7) } ' infile


Last edited by ahamed101; 03-24-2014 at 06:01 AM..
# 4  
Old 03-24-2014
Thanks Ahamed.

Is there any way to BEGIN if line does not has n.
# 5  
Old 03-24-2014
Sorry I didn't quite understand what you meant.
# 6  
Old 03-24-2014
Quote:
Originally Posted by stew
Thanks Ahamed.

Is there any way to BEGIN if line does not has n.
This question doesn't make any sense. The statements in a BEGIN clause are processed before any input lines are read.
# 7  
Old 03-24-2014
The query I posted earlier Begin if the line has 'n'. So I want same begine should happen if the line is not equal to 'n' (doesn't has 'n')
 
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