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:

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:

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.

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

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.

1 Like

You are 100% right about that. So we have no argument. :slight_smile: 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.

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, ...

1 Like

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.

Here are a few continue and break examples from source code:

  for (absNdx_NotAtom = 0; absNdx_NotAtom < absNdx1_Atom; absNdx_NotAtom++) {
    pckNdx = RWV_GEO_Abs_PckNdx___Array_g [absNdx_NotAtom];
    if (pckNdx < 0) {
      continue; // NOT picked
      }
    /* Many additional processing steps */
    }
    for (rowNdx = 0; rowNdx < rowCntOmitSum; rowNdx++) { // Do sum
      if (possiblySkipRow_IfAgeAdjustTicVar == TRUE) {
        if (RowVarNdx_EvtArrays_g == cryVarNdx_Age) {
          if (i64Array == RWA_I64_Pop_Denom________g) {
            if (OmitRowFromPopTotals_g [rowNdx] == TRUE) {
              continue;
              }
            }
          }
        }
      /* Many additional processing steps */
      }
  while (1 == 1) {
    eightBitNumber = (int) (numberToUse % 128);
    numberToUse /= 128;
    if (numberToUse > 0) {
      eightBitNumber |= 0X80;
      }
    binaryDataArray [0] = eightBitNumber;
    fwrite (binaryDataArray, 1, 1, binWtr);
    if (numberToUse == 0) {
      break;
      }
    }
  while (1 == 1) {
    items = fread (binaryDataArray, 1, 1, binRdr);
    eightBitNumber = binaryDataArray [0];
    sevenBitNumber = eightBitNumber & 0X7F;
    intRead += (sevenBitNumber * factorUsed);
    factorUsed *= 128;
    if ((eightBitNumber & 0X80) == 0) {
      break;
      }
    }

The isolated examples probably mean little or nothing to you. The point is programmers use break and continue all the time in situations not processing user input.

Here is a simple example using bash, showing how break and continue make sense:

while read field_1 field_2; do
  if [ $field_1 = "skip_me" ]; then
    continue
  fi
  if [ $field_1 = "bail_out" ]; then
    break
  fi
done

You are right that break and continue are often useful for processing user input. But there are obviously many kinds of input. Input from data files. Input from arrays. Input from strings. Input from numbers. Perhaps other inputs. Those other inputs also have to be read in some way, and break / continue are helpful for processing those other inputs too. There is no "special connection" between reading user input and continue/break.

Assume you have a loop, and you have a condition to stop executing that loop before it reaches its boundaries - break out in the middle of the loop. Use continue to skip the execution of the remaining body of the loop for that single iteration:

$ awk 'BEGIN {while (i++ < 100000) {if (i==3) continue; print i; if (i==5) break}}'
1
2
4
5
1 Like

The "program" in your 1st posting is not a valid shell script (although most shells will accept it without complaining. The only valid uses of break and continue are when they are used within a for loop, an until loop, or a while loop. If the script you posted:

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

is run using bash on OS X, it will either say:

filename: line 7: break: only meaningful in a `for', `while', or `until' loop

or:

filename: line 5: continue: only meaningful in a `for', `while', or `until' loop

By the logic you're using above and the shell program below:

printf "Wanna continue (y/n): "
read answer
if [ "$answer" = y ]
then    exit 0
else    printf "This script will continue.\n"
fi
... ... ...

there is a link between read, exit, and printf (or any other utilities you happen to execute if the value of a condition based on the value of a variable is true or false.

What I was saying about awk is that it can be used interactively or non-interactively. I can also say that bash, ksh, and perl can be used interactively or non-interactively. I have seen a sudoku game that reads the initial values from a file and then interactively reads commands from the user to set open positions to values supplied by the user (and optionally notes all values that could be entered into any unset position based on values that have already been set in that position's quadrant, row, and column). I have seen a similar sudoku game written in ksh93. I have a lot of shell scripts that perform non-interactive updates to homegrown databases (sometimes invoking awk to perform some of the work). Classifying awk, bash, ksh, or perl as interactive or non-interactive seems to me to just show a lack of imagination.

There are many non-interactive scripts where I would find all of the above tools to be bad choices depending on what the script is supposed to do. There are many interactive scripts where I would find all of the above tools to be bad choices depending on what the script is supposed to do. There are many interactive and non-interactive scripts where any or all of the above tools would be good choices depending on what the script is supposed to do.

Look at the requirements for a project and choose appropriate tools to fulfill those requirements. Don't restrict the tools you have in your toolbox based on someone's predefined notion of how those tools are most frequently used.

And hanson44 has just supplied you with several real life examples of using break and continue in loops. Whether these uses are interactive or non-interactive depends on where the input is coming from and what else is going on in the code before and after the given fragments.

Don Cragun, I have intentionally written only the read,continue and break statements . I knew the code I wrote should be within a loop(while, for, until) then only 'continue' and 'break' makes sense. In awk also , if continue and break are written without loop, it will show error. I knew all these. I thought a reader would understand that I am writing a sub-part of a loop. So, I intentionally skipped writing the loop (while or for). It should have been tacitly understood that the code I wrote is a part of a loop because then only what I am saying becomes meaningful. If 'loop' was not in my mind and simply I am writing 'continue' and 'break', it makes no sense of what I am saying.

By the way, thanks for the examples. I will go through later.

I tacitly understood the initial code was part of a loop. Personally, if I said anything too strong, I apologize. We're all just trying to help. Thank you for posting, and the discussion.

hanson44 , those isolated examples were more than enough to understand what I wanted to know. Thanks a lot!

Rudic, I got it from your simple code what I wanted. Thanks!!

In these kinds of fora, others can judge your level of understanding only through what you mention in your posts; rarely anyone knows anybody else personally to make any appropriate presumptions.
So, it's always better to mention such things explicitly rather than let others play a guessing game about your competence level.

4 Likes