[EMAIL PROTECTED] writes: > All, > > How can I print certain fields delimited by ' '? > In awk I would write awk '{print $1, $6}' filename > > Here is an out file that I want to grab data from : > > 04/29/04 11:00:28 [ 6687:ebexpire, [EMAIL PROTECTED] E00796 9840S 537 > 2B0234233543E6A4 > 04/29/04 11:00:28 [ 6687:ebexpire, [EMAIL PROTECTED] E00830 9840S 571 > D402325A8345ABDE > 04/29/04 11:00:28 [ 6687:ebexpire, [EMAIL PROTECTED] E00066 9840S 127 > 5202333193B75CBB > 04/29/04 11:00:28 [ 6687:ebexpire, [EMAIL PROTECTED] E00501 9840S 168 > 4B0233BABA5813F6 > > I want fields one two and six or the date, time and E string. > Does it matter whether I use a foreach or a while (<filehandle>) ?
Here is one way... other posters with more experience may show better ones. This way accesses a slice [elements 0,1,5] of the array represented in each line by way of the split function. It splits those elements into the vars on left hand side. In perl unlike awk, one needs to tell perl what you want it to do. Where as field splitting is done automatically by awk. The perl way actually allows more versatility but possibly more coding, as is nearly always the case between perl and awk. #!/usr/local/bin/perl -w my($fld1,$fld2,$fld6); open(FILE,"./yourdata") or die "Can't open FILE: $!"; while (<FILE>) { chomp; ($fld1, $fld2, $fld6) = (split(/\s+/,$_))[0,1,5]; print "<$fld1> <$fld2> <$fld6>\n"; } You could leave out the `chomp' and the printing of "\n" but I like to first remove newlines then add them back as needed to avoid surprises produced by newlines, sometimes at unexpected places. A whole other approach might be to use perls (included) a2p program that prints a perl script from an input awk script. Look at the ouput from running `a2p test.awk' to get some idea of what you'll get. Where test.awk looks like: cat test.awk #!/usr/bin/awk -f {print $1, $2, $6} -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>