awk doesn't understand 'read' statement!!!


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
awk doesn't understand 'read' statement!!!

While working on awk programming, i found that it doesn't understand 'read' statement. Then what's the use of 'continue' and 'break' statement in awk.
For ex:
Code:
awk '{k=1; while (k<10) {print $0; k++}}' emp.lst

Now, please say if I want to put the logic that after priting 1 line, it will ask for user's response to continue or not,is it possible. Like we use in shell script as below:

Code:
echo "Wanna continue (y/n):\c"
read answer
if [ "$answer" = y ]
then
continue
else
break
fi

Shell script understand 'read' statement and hence user has control on it.

What about awk? If awk doesn't understand 'read' statement then it means we can't give our input to it. Then what is the use of 'continue' and 'break' in awk.
# 2  
I don't understand how you got the idea that 'continue' and 'break' are somehow tied to user input. They are for flow control and are used whenever a program needs to exit a loop or skip the rest of the current iteration. You will find these types of statements in programs that do not even have a controlling terminal, much less interact with a user.

For reading user input, assuming it's on stdin, some awk's support /dev/stdin. You can use that with getline.

Regards,
Alister

Last edited by alister; 04-20-2013 at 02:34 PM..
# 3  
Yes, alister is right. "read" is very different from "break" and "continue".

Also, awk is very different from shell. One huge difference is that awk is not intended as an interactive environment.
# 4  
Quote:
Originally Posted by hanson44
Yes, alister is right. "read" is very different from "break" and "continue".

Also, awk is very different from shell. One huge difference is that awk is not intended as an interactive environment.
There is no reason why awk can't be used interactively. If you don't specify any input file for your awk program, it will read from standard input. If you need your awk program to read initialization data from a file and then interact with a user by reading standard input after that, use the name of the initialization data file as the first file operand and - as the next file operand.

When I'm testing small awk programs to see why some expression isn't working the way I expected, I frequently type in a small awk program and then just type input into it see what happens (and end it with ctl-D).

It is true that the shell and awk languages are different, but awk certainly has multiple ways to read and write files. Look at the awk man page on your system and search for the descriptions of functions like close, getline, print, printf, and system.

And as several people have said, break and continue have nothing to do with reading a file; they are used inside while and for loops for flow control in those loops.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Quote:
There is no reason why awk can't be used interactively.
You are 100% right about that. So we have no argument. Smilie But I did want to clarify that I never said "awk can't be used interactively". I said it's not intended as an interactive environment. I stand by that, because awk is normally used in a batch mode.

You are a very advanced user. So you are comfortable using awk in an interactive mode. And, as you say, you are normally testing when you run awk interactively. If you read again the original post, I'm sure you will agree the poster obviously has a VERY confused understanding, is at a pretty beginner level, because the poster thinks read and break are related. We all started at a beginner level, at one time or another. When explaining something to a beginner user, I think it's best to stick with the basics. We have to walk before we can run.
# 6  
Awk is awk, it has own syntax. Something same as in C, but lot of own, whole rule for block idea is something different.

Shell is shell and it has own syntax.

Awk is not part of shell. It's own language - program. Just like perl, php, python, ... has own syntax.

As hanson44 said, usually awk is used to handle large text data. It's nice tool for that. But in shell script using awk to split/calculate/... simple data, awk is not so nice tool - even syntax is nice. It's possible to use interactive but I have never seen/used. Shell is much better for interactive handling. Or Perl, php, python, ... Awk is fast data line/line handler without user waiting.

Awk is "huge" command if comparing ex. shell builtin properties or small "one problem commands" like cut, bc, grep, sort, ...
This User Gave Thanks to kshji For This Post:
# 7  
Don Cragun, that may be a good way to test our programs by giving standard input (without specifying any file name). I had run awk programs by giving stnd input.

Now, what I want to say is:
As many told that why I see a link between 'read' and 'continue/break', I also wondered why they wrote so. Because my 1st post reflects the connection. Based on the user input read by the read statement, either 'continue' is executed or 'break' is executed. See my codes, if user inputs 'y' , 'continue' is executed orelse 'break'. I hope this should show the connection.

Now , coming to the other point :--" what is the use of 'continue/break' if awk doesn't understand 'read' statement". You all are right to say that 'continue/break' is used in awk without user interference or without user input. The reason I told becz I have used programs which execute 'continue or break' based on the input as the example I gave you. so, I thought that if user input is not to be used, then the code doesn't need continue or break. If possible, may I expect anyone to give a small algorithm or example to show how without user input, 'continue/break' makes sense.

Alister thanks to say a way of reading stnd input. I will check how it works.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #888
Difficulty: Easy
sed was developed from 1973 to 1974 by Bill Gates of Microsoft.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script doesn't understand if conditiojn with or

I Bourne shell I have a lines #!/bin/sh something for file in *_${Today}*.csv *_${Today}*.txt do if then echo "This file will be processed in separate script" continue fi something done The script doesn't understand if... (6 Replies)
Discussion started by: digioleg54
6 Replies

2. Shell Programming and Scripting

How come this if statement doesn't work?

greetings, the following code isn't working as i expect it to. the first dbl brackets do but the second set gets ignored. ie: if i'm on t70c6n229 it echoes "Something" and i expect it not to. what am i missing? if " ]] || " ]]; then echo "Something" fi thanx! (9 Replies)
Discussion started by: crimso
9 Replies

3. Red Hat

Grep doesn't understand escape sequence?

I ran the following grep and sed command. grep "\t" emp.txt sed -n '/\t/p' emp.txt grep treated the '\' as to escape t and took the pattern as literal t whereas sed took the pattern as tab. That means , grep doesn't understand escape sequence!!!!!! what to do to make grep... (8 Replies)
Discussion started by: ravisingh
8 Replies

4. UNIX for Dummies Questions & Answers

How to read and understand Sendmail 8.14 log files ?

Can some one please help me know how exactly to understand logs in sendmail 8.14.5. I am pretty new to sendmail. I am running rhel6 on my system and i installed sendmail on it. Its just a test enivornment and i am sending and receiving emails on my localshot only. Jun 13 18:32:38 serv1... (0 Replies)
Discussion started by: Rohit Bhanot
0 Replies

5. Shell Programming and Scripting

My if statement doesn't work why?

I have the following and for some reason I can't have two options together. I mean if I choose -u and -p it won't work... why? #!/bin/bash resetTime=1 mytotalTime=0 totalHour=0 totalMin=0 averagemem=0 finalaverage=0 times=0 function usage() { cat << EOF USAGE: $0 file EOF } (10 Replies)
Discussion started by: bashily
10 Replies

6. AIX

How to read or understand errors in errpt

Hello, after upgrading the memory to 96GB & 6 Dual Processor for P 550 ( and still not applied the parameters which some experienced posters said in post https://www.unix.com/aix/141835-oracle-database-running-slow-aix-nmon-topas-6.html ) I am getting system dumps. How to understand and... (1 Reply)
Discussion started by: filosophizer
1 Replies

7. Shell Programming and Scripting

Not able to understand what's do the statement

Always, I have a confuse/not understand file descriptors. I know about the 0 for standard input 1 for standard output 2 for standard error and 3 to 9 is a additional file descriptor but it's a theoritical knowledge. Could you please give information about below two lines of code in AIX... (1 Reply)
Discussion started by: div_Neev
1 Replies

8. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 Replies

9. UNIX for Dummies Questions & Answers

my case statement doesn't work..

CO UNixware 7.1.1 Hi friends, I have chopped my case statementt out of my .profile and put it in another script called setsid. The case statement works when run from my .profile but not from my setsid file. All that the script does is set an environmental variable based on user input.... (7 Replies)
Discussion started by: sureshy
7 Replies

Featured Tech Videos