Last word of lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Last word of lines
# 8  
Old 01-31-2005
Awk processes the BEGIN section before trying to open the file.
Code:
$ rm file1

$ awk 'BEGIN{print "before"}{print "during"}END{print "after"}' file1
before
awk: fatal: cannot open file `file1' for reading (No such file or directory)

If your file is empty, then both the BEGIN and END sections are preocessed.
Code:
$ touch file1

$ awk 'BEGIN{print "before"}{print "during"}END{print "after"}' file1
before
after

There only one condition where the END section is processed but the BEGIN section is not processed: you have miss-spelled "BEGIN".
# 9  
Old 02-01-2005
Thanks,

Got it.
What happened was my script was
BEGIN {} {}
I was printing something in the second brace which now comes in the scope of file, thats why it was not printing when the file was empty.

But now i have another problem.
Can i print variables in BEGIN clause.
because on combining the 2 braces my variables are not printing.

My script is:
awk -f file.awk a=xyz b=xyz file.txt
# 10  
Old 02-01-2005
Code:
awk -v a=xyz 'BEGIN{print "before"; print a}{print "during"}END {print "after"}'  file1

# 11  
Old 02-01-2005
Yes,

I am doing the same thing.
awk -v a=xyz 'BEGIN{print "before"; print a}{print "during"}END {print "after"}'

This doesn't print the variable a;

But on doing
awk -v a=xyz 'BEGIN{}{print "before"; print a}{print "during"}END {print "after"}'
or
awk -v a=xyz 'BEGIN{print "before"}{print a}{print "during"}END {print "after"}'

i.e. taking variable printing block outside of BEGIN scope it prints.
# 12  
Old 02-01-2005
Use the -v option to set a variable before the BEGIN section is processed...
Code:
$ awk -v a=xyz 'BEGIN{print "before a=" a}'
before a=xyz

You can also set a variable before each input file...
Code:
$ echo "line1" > file1

$ awk -v a=xyz 'BEGIN{print "before a=" a}{print "during a=" a}' a=123 file1
before a=xyz
during a=123

# 13  
Old 02-02-2005
Thanks got it,

Mine awk doesn't support -v option.
I had to use nawk.

How is nawk different from awk.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. UNIX for Dummies Questions & Answers

Delete lines with a word and their above lines

Hi, i have a file like this: A1 kdfjdljfdkljfdlf A2 lfjdlfkjddkjf A3 ***no hit*** A4 ldjfldjfdk A5 ***no hit*** A6 jldfjdlfjdlkfjd I want to remove the lines "***no hit*** and their above line to get an output file like this: (11 Replies)
Discussion started by: the_simpsons
11 Replies

4. UNIX for Dummies Questions & Answers

How to grep a word and the two lines before?

Hi I have this txt file getting from a lpstat command. XA40 XA40 0 Unknown 0 0 1 1 1 0 Unknown LPD 0 0 1 1 2 0 Unknown specified 0 0 1 1 3 XA99 @spip READY : (FATAL ERROR) 0781-233 Unknown host spiprs01.mon.local. XA01 @xs00 READY XA01 XA01 0 Unknown 0 0 1 1 1 0 Unknown LPD 0 0 1 1... (5 Replies)
Discussion started by: npatao71
5 Replies

5. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

6. Shell Programming and Scripting

deleting lines above and below a word!

i jst want to delete a host entry from httpd.conf for eg: i have entries such as: <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik ServerName kar </VirtualHost> <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik1 ServerName www </VirtualHost> <VirtualHost... (4 Replies)
Discussion started by: jacky29
4 Replies

7. Shell Programming and Scripting

grep lines containing a word only twice.

Hi, I have a query on using grep options. I tried with several options but unable to do it. lanite:52> cat note 123 456 ab 123 ab cv 234 4566 67 ab gh tij ab 12 34 ab ab cv dfgv ab cv ab kjhk ab ghj sdf dfg ab jljklj ab Now, I need to use grep to find the line which contains the... (6 Replies)
Discussion started by: dilipr25
6 Replies

8. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

9. UNIX for Dummies Questions & Answers

append word to lines

Hi all, Can someone suggest how to append some word to all lines in file. for example word "Honey" to file f1 with lines: Mia Katrin Elizabeth to get Honey Mia Honey Katrin Honey Elizabeth Thanks in advance Givi (3 Replies)
Discussion started by: giviut
3 Replies
Login or Register to Ask a Question