Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Simple awk question

by czah7 (Initiate)
on Jun 05, 2014 at 16:59 UTC ( #1088881=perlquestion: print w/replies, xml ) Need Help??

czah7 has asked for the wisdom of the Perl Monks concerning the following question:

I am very new to PERL. Like first week I've tried using it. In bash I can do this:

echo hello1 hello2 hello3|awk '{ print $1 }' and it prints "hello1"

In a script I need to do a similar thing, but the data is inside a variable.

For example: echo $variable |awk '{ print $1 }'

I've tried several variations, I know I'm missing something very simple...

$time = print $error|print $F[0]; or $time = system(echo $error) | system( awk '{print $1}'); or $time = print $error | system(awk '{ print $1 }');

None work. So to make it simple.
#!/usr/bin/perl $error = '2014-06-04T15:24:21-05:00 syslog_dp [0x80e00099][ftp][error] + secure-backup(FBB): trans(5487)'; $time = print $error|print $F[0];
$time should then = 2014-06-04T15:24:21-05:00

Replies are listed 'Best First'.
Re: Simple awk question
by no_slogan (Deacon) on Jun 05, 2014 at 17:05 UTC

    Perl has awk functionality built in, so you can do something like:

    $x = "ack thpt barf"; @fields = split " ", $x; print $fields[0], "\n";
      I think this is exactly what I need! Thanks.

        On second thought. Almost! I need $fields to now be just that first field. Not just print the first field, it now needs to be a new variable.

        I tried
        $fields = print $fields[0], "\n";
        close but no cigar, yet.
Re: Simple awk question
by Bloodnok (Vicar) on Jun 05, 2014 at 17:26 UTC
    How about echo $variable | perl -nae 'print $a[0]' or possibly perl -e 'print((split /\s+/, "'$variable'")[0])' ?

    Update:

    Very many thanx to LanX - modifed 2nd suggestion to correct parenthesis, but ignored nested quotes comment since they are most definitely deliberate - without them, perl would split the string '$variable' whereas with them, the shell expands the shell variable and perl then splits that string.

    A user level that continues to overstate my experience :-))
      perl -e 'print (split /\s+/, "'$variable'")[0]'

      nested quotes? :)

      update

      and the brackets belong to print , (...)[0] won't work.

      Cheers Rolf

      (addicted to the Perl Programming Language)

Re: Simple awk question
by LanX (Sage) on Jun 05, 2014 at 17:08 UTC
    Perl doesn't know echo only print

    What you seem to want is open with a pipe-symbol╣ to write to awk's STDIN.

    Calling awk from Perl is somehow strange, though, Perl was primarily designed to replace awk and sed .

    update

    if you just wanna pass a handful of variables better use arguments in backticks

     my $result = `awk $var1 $var2`;

    but I don't know much about awk (cause I don't need to ;-)

    Cheers Rolf

    (addicted to the Perl Programming Language)

    ╣) like in

    open(my $awk, "|awk >Tmp$$") # $$ is our process id or die "Can't start awk: $!"; print $awk $var1, $var2; close $awk;
      Calling awk from Perl is somehow strange, though, Perl was primarily designed to replace awk and sed .

      For what it's worth, there is the a2p tool to convert AWK scripts into Perl. It can be helpful in transitioning to Perl from AWK.

Re: Simple awk question
by GotToBTru (Prior) on Jun 05, 2014 at 18:13 UTC

    Please provide enough of your perl code to show what the variables are, the input, and what you expect the output to look like.

    echo $variable | awk '{print $1}'

    is redundant. Save yourself some typing.

    echo $variable

    Okay, you are probably trying to do more than just print a single variable, but that's all I have to go on from what you've given me so far. Well, there's an array called @F and scalars called $time and $error, but it's anyone's guess as to what they contain or what they are for.

    1 Peter 4:10
      So to make it simple.
      #!/usr/bin/perl $error = '2014-06-04T15:24:21-05:00 syslog_dp [0x80e00099][ftp][error] + secure-backup(FBB): trans(5487)'; $time = print $error|print $F[0];
      $time should then = 2014-06-04T15:24:21-05:00
        $time = print $error|print $F[0];

        That attempt to pipe one statement to another statement like you would do when piping shell commands won't work.

        You've already been shown a couple ways to extract the datestamp.

        $error = '2014-06-04T15:24:21-05:00 syslog_dp [0x80e00099][ftp][error] + secure-backup(FBB): trans(5487)'; $time = (split /\s/, $error)[0]; print $time, "\n"; # or you could do this: ($time) = $error =~ /^(\S+)/; print $time, "\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1088881]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-05-18 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?