Find the answer to your Linux question:
Results 1 to 2 of 2
I have a problem with awk.. Is it possible to create a script in AWK that reads that input of a user from a keyboard.. Something like: Prompts the user ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    15

    AWK scripts..


    I have a problem with awk..

    Is it possible to create a script in AWK that reads that input of a user from a keyboard..

    Something like:
    Prompts the user for some string input and stores this string into a variable which will be use in the process.. ? ..

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi, ethnicme.

    Yes it is possible, although it may not be obvious. One way is illustrated here:
    Code:
    #!/usr/bin/awk -f
    #!/usr/bin/nawk -f
    #!/usr/bin/mawk -f
    #!/usr/bin/gawk -f
    
    # @(#) a4       Demonstrate awk writing to, reading from terminal.
    # Note that the "-f" is required in the sh-bang line.
    # Use: ./a4 file  -or-  ./a4 /dev/null
    # Verified with awk, nawk, gawk, mawk.
    
    BEGIN   { print "System " ARGV[0] ": enter your name" > "/dev/tty"
                    getline name < "/dev/tty"
            }
            { print $0 }
    END     { print "I read your name as: " name > "/dev/tty" }
    When this is run with an existing trivial file:
    Code:
    % ./a4 data1
    System awk: enter your name
    drl
    Hello, world.
    I read your name as: drl
    Best wishes ... cheers, drl

    PS Questions like this will probably get more exposure in the Linux Programming & Scripting forum.

    ( edit 1: add PS )
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •