×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help converting Perl Script to Awk script

Help converting Perl Script to Awk script

Help converting Perl Script to Awk script

(OP)
Hello guys, I could really use some help. I'm trying to learn AWK but it wasn't at all as easy as I thought it would be. I have a really simple Perl script, that prints a specified text file, as well as the line number next to it. The only difference I would like to make, is that I specify the file as an argument at the command line instead of the program asking me for it. How would I go about doing this and making an AWK script? Here's my Perl script:

CODE

#!/usr/local/bin/perl
    use warnings;
    use strict;

    print "Specify the file you want to look at:\n";
    my $file_name = <STDIN>;
    chomp $file_name;

    open(FH, '<', $file_name) or die "Cannot open $file_name: $!";
    print "This is the listed content of: $file_name\n";

    my $line_n = 1;
    while(<FH>){
    print "$line_n. $_";
    $line_n++;
}

    close(FH); 
Sorry if I'm breaking any forum rules, this is my first post. Any advice or tips is greatly appreciated. Thanks in advance.

RE: Help converting Perl Script to Awk script

Hi

CODE --> command line

awk '$0=NR". "$0' /path/to/file 

BTW, the above is the equivalent of this :

CODE --> command line

perl -pe '$_="$.. $_"' /path/to/file 

BTW, if the dot after the number is not mandatory, see whether you like this more :

CODE --> command line

cat -n /path/to/file 

Quote (JanneJannesson)

Sorry if I'm breaking any forum rules, this is my first post.
Not really breaking rules, but Thread Type should be Question, not Helpful Tip. ( You can not change it yourself, but if you click the Report link at the bottom of your post, then you can ask forum management to change it. )
And we used to write pieces of code between [code] and [/code] TGML tags to improve readability.

Feherke.
feherke.github.io

RE: Help converting Perl Script to Awk script

(OP)
Hi, thank you for your response, I flagged my own post as well as added the code tags.

I tried your first bit of code, and it worked great in the command line. However --
Perhaps I was unclear in my original post, what would the awk script equivalent be to your first row of code?

Quote (feherke)

CODE

awk '$0=NR". "$0' /path/to/file 
I want to be able to run the script from the command line like so(or something similar):

CODE

awk 'test.awk' test.txt 

Hope this made things clearer, if not tell me and I'll try and clear things up.

RE: Help converting Perl Script to Awk script

Hi

Not exactly like that, awk expects the 1st parameter to be code. So if you pass it a script file name instead, then you have to tell it :

CODE --> command line

awk -f test.awk test.txt 
In which case the content of test.awk is only what we passed earlier in the command line :

CODE --> test.awk

$0=NR". "$0 

Alternatively ( assuming you are on a Unix or Unix-like operating system ) you could set execute permission on the test.awk file and use it like this :

CODE --> command line

./test.awk test.txt 
In which case the content of test.awk has to contain a shebang ( user the actual path to awk on your system ) too :

CODE --> test.awk

#!/usr/bin/awk -f
$0=NR". "$0 

Feherke.
feherke.github.io

RE: Help converting Perl Script to Awk script

(OP)
Hello!
Thank you for your quick response! This is exactly what i was seeking. AWK seems to be really handy once you get used to it. Literally one row of code instead of 10 in Perl.
You really cleared up a few things for me, including how to actually run the AWK script, I read somewhere that single quotes: ' were necessary. For some reason I also thought I needed to use the BEGIN and END command, which obviously I didn't need to. So simple. Thank you, once again.

RE: Help converting Perl Script to Awk script

But why reinvent the wheel. the cat processor was written to dump a file to the screen

cat -n /path/to/file

Bill
Lead Application Developer
New York State, USA

RE: Help converting Perl Script to Awk script

(OP)
Why not? The actual purpose of this wasn't for the scripts sake, it was to try and get a basic understanding of awk.

RE: Help converting Perl Script to Awk script

true

Bill
Lead Application Developer
New York State, USA

RE: Help converting Perl Script to Awk script

Hi

Quote (JanneJannesson)

The actual purpose of this wasn't for the scripts sake, it was to try and get a basic understanding of awk.
Then I am afraid my answer was not that helpful. sad Let me try to explain.

Awk code is built from pattern { action statements } pairs, where action statements get executed only if the pattern evaluates to true. With the exception of some special patterns all such pattern { action statements } pairs get executed for each input record.

Both pattern and action statements can be left out ( but not in the same time ), in which case their default are used :
  • default pattern is 1, which being a logical true, means always
  • default action statements is {print}, which being equivalent with {print $0} prints out the current record

CODE

$0 = NR ". " $0
# actually means :
$0 = NR ". " $0 { print }

# alternatively we can put our code in the action part and use no pattern :
{ print NR ". " $0 } 
( As we set the current record ( $0 ) to current record number ( NR ) concatenated with a piece of string and the current record, the result can never be false, so will always be printed. )

Here is a close rewrite of your original Perl code :

CODE --> Awk

#!/usr/bin/awk -f

BEGIN {
    print "Specify the file you want to look at:"
    getline file_name

    print "This is the listed content of: " file_name

    line_n = 1
    while (success = getline < file_name) {
        if (success == -1) {
            print "Cannot open " file_name ": " ERRNO > "/dev/stderr"
            exit 1
        }

        print line_n ". " $0
        line_n++
    }

    close(file_name)
} 
Some explanations :
  • The BEGIN is a special pattern, means the action gets executed before processing the input file. As in this version there is no input file specified as command line parameter, there will be no automatic input processing. So we must use BEGIN to have our code executed.
  • There is no explicit file opening. The file gets opened when reading from it for the 1st time. So we can do the error checking only after the getline call.
  • There is no die, the closes equivalent is to print to stderr then to exit with a non-zero exit code

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close