awk vs perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk vs perl
# 1  
Old 02-05-2010
awk vs perl

Code:
awk -F "|" '{print $2$3$4 upto $30}' file1 > file2

Same logic, i want to write it in perl

I tried
Code:
#!/bin/usr/perl 
my $line;
open FH, "<file1" or die " Can't open file $!";
open FH1, ">file2" or die "Can't open file ";
while (<FH1>){
    $line = (split /\|/,$_)[1,2,3,4....];
    print FH2 $line;
}
close(FH1);
close(FH2);

It didn't work like awk, what i want..
Please guide me
please tell which one is good in speed and performance in a a large file

Last edited by Franklin52; 02-05-2010 at 03:02 PM.. Reason: Please use code tags!!
# 2  
Old 02-05-2010
in perl it is more simple than your awk & perl codes:-

Code:
perl -nalF'\|' -e '$"="|" ; print "@F[1..29]" ;'  infile.txt

SmilieSmilieSmilie

Last edited by ahmad.diab; 02-05-2010 at 04:42 PM..
# 3  
Old 02-06-2010
Need to understand

could you Please tell how it's work. It's very complex but may be effective. Someone please help me to understand this code
# 4  
Old 02-07-2010
Lightbulb

Quote:
Originally Posted by pritish.sas
could you Please tell how it's work. It's very complex but may be effective. Someone please help me to understand this code
See 'man perlrun', and check out the options -n -a -l -F and -e.

$" is also known by its long name $LIST_SEPARATOR (see 'man perlvar'), which explains a lot I guess ;-)

'[1..29]' is an array slice, and selects all specified elements. '1..29' is a range of all integers between and including 1 and 29.

Hope that helps...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting awk to perl

Hello. I'm trying to convert an awk script I wrote to perl (which I just started self-teaching). I tried the a2p command but I couldn't make sense of most of it. Here was the awk code: BEGIN{ FS = "," print "NAME\tLOW\tHIGH\tAVERAGE" a=0 } { if(a==0){ a+=1 (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

3. Shell Programming and Scripting

awk and perl grouping.

Hello folks. After awk, i have decided to start to learn perl, and i need some help. I have following output : 1 a 1 b 2 k 2 f 3 s 3 p Now with awk i get desired output by issuing : awk ' { a = a FS $2 } END { for ( i in a) print i,a }' input 1 a b 2 k f 3 s p Can... (1 Reply)
Discussion started by: Peasant
1 Replies

4. Shell Programming and Scripting

get value between <abc and > by perl, awk

Hi Everyone, cat 1.txt a <abc b vfff c 000> d 4444 the output is: <abcvfff000> by using perl or awk, can get the value betwee "<abc" and ">", assume 1.txt has lots of those tags, so the output can filter out all those values. Please advice. Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

5. Shell Programming and Scripting

awk - to -- PERL

DEAR ALL, i am using following command to fetch some records from more then 50000 files... command is taking more time .... can i have perl command for the same...( i am New to perl ) awk -F "|" '{ if($4==3244898 && $5==5000185) print $66}' *.DATA (2 Replies)
Discussion started by: arvindng
2 Replies

6. Shell Programming and Scripting

Using perl grep and awk

I have a script to get server information i wrote in perl because i would like to learn it (and I use it for work). It works great, however i would like to know if there is a good way to reduce the following line. Sean (6 Replies)
Discussion started by: insania
6 Replies

7. Shell Programming and Scripting

awk or perl

hi all I am new to awk/perl I would appreciate it if you could help! how do I read a sub folder and their files. e.g. simulation/10ms/router1.dat, router2.dat,..., router16.dat simulation/100ms/router1.dat, router2.dat,..., router16.dat simulation/300ms/router1.dat,... (14 Replies)
Discussion started by: mmoses
14 Replies

8. Shell Programming and Scripting

awk command with PERL

Hi All, My Input file looks like below: Input: 100,200,300 $fw=`head -1 test.csv | awk -F, '{print \$1}'`; $fw="'$fw" $fw="$fw'" print $fw Output:'100' I want the first field to be printed in single quotes ('') like above. I could get the ouptput but the problem is single... (6 Replies)
Discussion started by: kmkbuddy_1983
6 Replies

9. Shell Programming and Scripting

awk to perl convert

Dear Collegue Do anybody help me to convert this AWK script to Perl script { for (i = 2; i <= length ($0); i++) { x = substr($0, i , 1) if (c > 0) { b = b x if (x == "(") c++ ... (2 Replies)
Discussion started by: jaganadh
2 Replies

10. Shell Programming and Scripting

AWK embeded PERL

Hello Guys, I am refering to this great forum once again for help. After various attempts at this, I am still failing to obtain the desire effect. I have to write a perl script which: 1- reads two values from the user (e.g name & passwd) 2- check each value against a file containing the... (7 Replies)
Discussion started by: bionicfysh
7 Replies
Login or Register to Ask a Question