Almost two Hundred of the Most UseFul Perl One-liners
193 of the Most Useful Perl One Liners and Counting
What is Perl?
Perl is an open source, highly capable, feature-rich programming language with over 30 years of development.
- This Perl one-liner replaces all occurrences of the text
foo withbar in the file perl.txt.
cat perl.txt
foo foo foo
bar bar bar
bar bar baz
foo foo baz
perl -pi -e 's/foo/bar/g' perl.txt
cat perl.txt
bar bar bar
bar bar bar
bar bar baz
bar bar baz
- If you want to make a copy of perl.txt before Perl executes user the following one-liner.
perl -pi.bak -e 's/foo/bar/g' perl.txt
cat perl.txt.bak
foo foo foo
bar bar bar
bar bar baz
foo foo baz
cat perl.txt
bar bar bar
bar bar bar
bar bar baz
bar bar baz
- The next Perl one-liner will replace
foo withbar in files 1.txt & 2.txt.
cat 1.txt
foo foo foo
baz baz baz
linux linux linux
cat 2.txt
foo foo foo
baz baz baz
linux linux linux
perl -pi -e 's/foo/bar/g' 1.txt 2.txt
cat 1.txt
bar bar bar
baz baz baz
linux linux linux
cat 2.txt
bar bar bar
baz baz baz
linux linux linux
- This next Perl one-liner will replace
foo withbar ; but only if the line matcheslinux .
cat 1.txt
foo foo foo
bar bar bar
linux foo foo
perl -pi -e 's/foo/bar/g if /linux/' 1.txt
cat 1.txt
foo foo foo
bar bar bar
linux bar bar
- Building on our example the next Perl one-liner will replace
foo withbar only if the line contains a digit.
cat 1.txt
foo foo foo
1 foo foo foo
foo foo 1 foo
perl -pi -e 's/foo/bar/g if /\d/' 1.txt
cat 1.txt
foo foo foo
1 bar bar bar
bar bar 1 bar
- This Perl one-liner will find all the lines that have been repeated.
cat 1.txt
linux is cool
linux is cool
linux is cool
BSD is also cool
BSD is also cool
penguins
perl -ne 'print if $a{$_}++' 1.txt
linux is cool
linux is cool
BSD is also cool
- The next Perl one-liner will print the line number before the text in each line.
cat 1.txt
Linux is cool
Penguins are cool
BSD is cool
Gerald is cool
perl -ne 'print "$. $_"' 1.txt
1 Linux is cool
2 Penguins are cool
3 BSD is cool
4 Gerald is cool
- This next Perl one-liner only numbers lines that repeat. NOTE: It doesn't include the original line.
cat 1.txt
Linux is cool
Linux is cool
Linux is cool
BSD is cool
Gerald is cool
perl -ne 'print "$. $_" if $a{$_}++' 1.txt
2 Linux is cool
3 Linux is cool
- Print the date 90 days from today's date.
perl -MPOSIX -le '@t = localtime; $t[3] -= 90; print scalar localtime mktime @t'
Fri Sep 23 11:52:48 2022
- Generate an 8 letter password.
perl -le 'print map { ("a".."z")[rand 26] } 1..8'
orgzhpqc
- Find the decimal ( e.g. "network" big-endian) number that corresponds to an IP address.
perl -le 'print unpack("N", 127.0.0.1)'
2130706433
- Sum the number in the first column of a table.
cat nums.txt
1 2 3 4
2 3 4 5
3 4 5 6
cat nums.txt |perl -lane '$sum += $F[0]; END { print $sum }'
6
- Find out how many packets have passed through iptables. Obviously this assumes you're currently running an iptables based firewall.
sudo iptables -L -nvx | perl -lane '$pkts += $F[0]; END { print $pkts }'
2395859
- Generate a list of all users on a UNIX based system.
perl -a -F: -lne 'print $F[4]' /etc/passwd
- Double-space a file.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -pe 's/$/\n/' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Add a newline at the end of each line in a file.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -nE 'say' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Double-space a file except for empty lines.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -pe '$_ .= "\n" unless /^$/' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Triple lines in a file.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -pe 's/$/\n\n/' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Insert 5 new lines after each line in a file.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -pe '$_ .= "\n"x5' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Add a blank line before every line.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -pe 's/^/\n/' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Remove all blank lines.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -ne 'print unless /^$/' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Remove all consecutive blank lines; leaving only one.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -00pe0 test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Compress/expand all blank lines into 2 consecutive blank lines.
cat test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
perl -00 -pe '$_ .= "\n"x2' test.txt
Linux, Perl, & OSS software rock!
BSD is also cool!
Rust also rocks!
- Double-space between all words.
cat test.txt
Penguins are cool and Tux is cute & fluffy!
perl -pe 's/ / /g' test.txt
Penguins are cool and Tux is cute & fluffy!
- Remove all spacing between words.
cat test.txt
Penguins are cool and Tux is cute & fluffy!
perl -pe 's/ +//g' test.txt
PenguinsarecoolandTuxiscute&fluffy!
- Convert all spacing between words to a single space. NOTE: This will fail on tabs!
cat test.txt
Penguins are cool and Tux is cute & fluffy!
perl -pe 's/ +/ /g' test.txt
Penguins are cool and Tux is cute & fluffy!
- Insert a space between all characters.
cat test.txt
Linux is awesome! Open source is cool!
perl -lpe 's// /g' test.txt
L i n u x i s a w e s o m e ! O p e n s o u r c e i s c o o l !
- Number all lines in a file.
cat test.txt
Linux is cool!
Penguins are awesome!
OpenBSD is secure!
perl -pe '$_ = "$. $_"' test.txt
1 Linux is cool!
2 Penguins are awesome!
3 OpenBSD is secure!
- Number only non-empty lines in a file.
cat test.txt
Linux is cool!
Penguins are awesome!
OpenBSD is secure!
perl -pe '$_ = ++$x." $_" if /./' test.txt
1 Linux is cool!
2 Penguins are awesome!
3 OpenBSD is secure!
- Number and print only non-empty lines in a file.
cat test.txt
Linux is cool!
Penguins are awesome!
OpenBSD is secure!
perl -ne 'print ++$x." $_" if /./' test.txt
1 Linux is cool!
2 Penguins are awesome!
3 OpenBSD is secure!
- Number all lines but print line numbers only for non-empty lines.
cat test.txt
Linux is cool!
Penguins are awesome!
OpenBSD is secure!
perl -pe '$_ = "$. $_" if /./' test.txt
1 Linux is cool!
4 Penguins are awesome!
5 OpenBSD is secure!
- Number only lines that match a pattern; print others unmodified.
cat test.txt
Linux is cool!
OpenBSD is awesome!
UNIX is rock solid!
perl -pe '$_ = ++$x." $_" if /OpenBSD/' test.txt
Linux is cool!
1 OpenBSD is awesome!
UNIX is rock solid!
- Number and print only lines that match a pattern.
cat test.txt
Linux is cool!
Linux is awesome!
Penguins are cool!
perl -ne 'print ++$x." $_" if /Penguins/' test.txt
1 Penguins are cool!
- Number & print only lines that match a pattern.
cat test.txt
Linux is cool!
Linux is awesome!
Penguins are cool!
perl -ne 'print ++$x." $_" if /Linux/' test.txt
1 Linux is cool!
2 Linux is awesome!
- Number all lines but print line numbers only for lines that match a pattern.
cat test.txt
penguins are cool!
penguins are cute!
BSD devils are red.
perl -pe '$_ = "$. $_" if /penguin/' test.txt
1 penguins are cool!
2 penguins are cute!
BSD devils are red.
- Number all lines in a file using a custom format. This aligns the line numbers 5 positions to the left. To learn more about various formats available run perldoc -f sprintf.
cat test.txt
penguins are cool!
penguins are cute!
BSD devils are red.
perl -ne 'printf "%-5d %s", $., $_' test.txt
1 penguins are cool!
2 penguins are cute!
3 BSD devils are red.
- Print the total number of lines in a file (e.g. emulate wc -l).
cat test.txt
penguins are cool!
penguins are cute!
BSD devils are red.
perl -lne 'END { print $. }' test.txt
3
- Print the number of non-empty lines in a file.
cat test.txt
penguins are cool!
penguins are cute!
perl -le 'print scalar(grep { /./ } <>)' test.txt
2
- Print the number of empty lines in a file.
cat test.txt
penguins are cool!
penguins are cute!
perl -lne '$x++ if /^$/; END { print $x+0 }' test.txt
3
- Print the number of lines in a file that match a pattern (e.g. emulate grep -c).
cat test.txt
penguins are cool!
penguins are cute!
BSD is the devil!
perl -lne '$x++ if /BSD/; END { print $x+0 }' test.txt
1
- Number words across all lines.
cat test.txt
penguins are cool!
penguins are cute!
BSD is the devil!
perl -pe 's/(\w+)/++$i.".$1"/ge' test.txt
1.penguins 2.are 3.cool!
4.penguins 5.are 6.cute!
7.BSD 8.is 9.the 10.devil!
- Number words on each individual line.
cat test.txt
penguins are cool!
penguins are cute!
BSD is the devil!
perl -pe '$i=0; s/(\w+)/++$i.".$1"/ge' test.txt
1.penguins 2.are 3.cool!
1.penguins 2.are 3.cute!
1.BSD 2.is 3.the 4.devil!
- Replace all words with their numeric positions.
cat test.txt
penguins are cool
penguins are cute
BSD is the devil
perl -pe 's/(\w+)/++$i/ge' test.txt
1 2 3
4 5 6
7 8 9 10
- Check if a number is a prime when user enters a number.
perl -lne '(1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is prime"'
5
5 is prime
- Print the sum of all fields on each line.
cat test.txt
1 2 4 5
perl -MList::Util=sum -alne 'print sum @F' test.txt
12
- Same as the above exmaple except split on :.
cat test.txt
1:2:4:5
perl -MList::Util=sum -F: -alne 'print sum @F' test.txt
12
- Print the sum of all fields on all lines.
cat test.txt
1 1
1 2 4
1 2 5
perl -MList::Util=sum -alne 'push @S,@F; END { print sum @S }' test.txt
17
- Shuffle all fields on each line.
cat test.txt
1 2 4 5 6 7 8
perl -MList::Util=shuffle -alne 'print "@{[shuffle @F]}"' test.txt
8 7 4 2 1 5 6
- Same as above but from the cmd line.
echo 1 2 4 5 6 7 8 | perl -MList::Util=shuffle -alne 'print "@{[shuffle @F]}"'
8 7 1 4 2 5 6
- Same as above except concatenate each field.
echo 1 2 4 5 6 7 8 | perl -MList::Util=shuffle -alne 'print shuffle @F'
8125467
- Same as above except change the separator when printed.
echo 1 2 4 5 6 7 8 | perl -MList::Util=shuffle -alne '$,=":"; print shuffle @F'
1:5:6:7:8:4:2
- Same as above excpet join each element of @F with a space.
echo 1 2 4 5 6 7 8 | perl -MList::Util=shuffle -alne 'print join " ", shuffle @F'
8 4 6 7 2 5 1
- Find the numerically smallest element (e.g. minimum element) on each line.
cat test.txt
1 5 99 105
0 5 99 1254
-28 2.5 29678
perl -MList::Util=min -alne 'print min @F' test.txt
1
0
-28
- Find the numerically smallest element (e.g. minimum element) over all lines.
cat test.txt
1 5 99 105
0 5 99 1254
-28 2.5 29678
perl -MList::Util=min -alne '@M = (@M, @F); END { print min @M }' test.txt
-28
- Find the numerically largest element (e.g maximum element) on each line.
cat test.txt
1 5 99 105
0 5 99 1254
-28 2.5 29678
perl -MList::Util=max -alne 'print max @F' test.txt
105
1254
29678
- Find the numerically largest element (e.g maximum element) over all lines.
cat test.txt
1 5 99 105
0 5 99 1254
-28 2.5 29678
perl -MList::Util=max -alne '@M = (@M, @F); END { print max @M }' test.txt
29678
- Replace each field with its absolute value.
cat test.txt
-22 4 0 1467
perl -alne 'print "@{[map { abs } @F]}"' test.txt
22 4 0 1467
- Print the total number of fields on each line.
cat test.txt
Hi
What's up?
Penguins are cool!
perl -alne 'print scalar @F' test.txt
1
2
3
- Print the total number of fields on each line; followed by the line.
cat test.txt
Hi
Penguins are cool!
What's up?
perl -alne 'print scalar @F, " $_"' test.txt
1 Hi
3 Penguins are cool!
2 What's up?
- Print the total number of fields on all lines.
cat test.txt
Hi
Penguins are cool!
What's up?
perl -alne '$t += @F; END { print $t }' test.txt
6
- Print the total number of fields that match a pattern.
cat test.txt
Hi
Penguins are cool!
What's up?
Penguins are cute!
perl -alne 'map { /Penguins/ && $t++ } @F; END { print $t || 0 }' test.txt
2
- Same as above but use a loop. On a large file you should see an increase in performance with this method.
cat test.txt
Hi
Penguins are cool!
What's up?
Penguins are cute!
perl -alne '$t += /Penguins/ for @F; END { print $t }' test.txt
2
- Another way to do the above is use grep instead.
cat test.txt
Hi
Penguins are cool!
What's up?
Penguins are cute!
perl -alne '$t += grep /Penguins/, @F; END { print $t }' test.txt
2
- Print the total number of lines that match a pattern.
cat test.txt
penguins are cool
BSD is a red devil
penguins are cute
penguins are fluffy
penguins are cool
penguins are cool and fluffy
perl -lne '/penguins are cool/ && $t++; END { print $t || 0 }' test.txt
3
- Print the number π to 20 decimal places.
perl -Mbignum=bpi -le 'print bpi(21)'
3.14159265358979323846
- You can also use the bignum library to print π to a precomputed 39 decimal places.
perl -Mbignum=PI -le 'print PI'
3.141592653589793238462643383279502884197
- Print the constant e to 20 decimal places.
perl -Mbignum=bexp -le 'print bexp(1,21)'
2.71828182845904523536
- Use the bignum to print the constant e to a precomputed 39 decimal places.
perl -Mbignum=e -le 'print e'
2.718281828459045235360287471352662497757
- Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC)
perl -le 'print time'
1681516140
- Print Greenwich Mean Time and local computer time.
perl -le 'print scalar gmtime'
Fri Apr 14 23:49:53 2023
- Same as above but return localtime.
perl -le 'print scalar localtime'
Fri Apr 14 19:50:59 2023
- In the list context, both gmtime and localtime return a nine-element list which looks like the following:
($second, [0]
$minute, [1]
$hour, [2]
$month_day, [3]
$month, [4]
$year, [5]
$week_day, [6]
$year_day, [7]
$is_daylight_saving [8]
)
You can slice this list & print individual elements like so:
perl -le 'print join ":", (localtime)[5,4,3]'
123:3:14
- You can also print the above as a range.
perl -le 'print join ":", (localtime)[1..3]'
1:20:14
- Print yesterday's date.
date
Fri 14 Apr 2023 08:04:01 PM EDT
perl -MPOSIX -le '@now = localtime; $now[3] -= 1; print scalar localtime mktime @now'
Thu Apr 13 20:04:05 2023
- Print the date 12 months, 8 days, and 5 seconds ago.
perl -MPOSIX -le '@now = localtime; $now[0] -= 5; $now[3] -= 8; $now[4] -= 12; print scalar localtime mktime @now'
Wed Apr 6 20:13:08 2022
- Calculate the factorial of 10.
perl -MMath::BigInt -le 'print Math::BigInt->new(10)->bfac()'
3628800
- Another way to calculate a factorial is to multiply the numbers from 1 to n.
perl -le '$f = 1; $f *= $_ for 1..10; print $f'
3628800
- Calculate the greatest common divisor.
perl -MMath::BigInt=bgcd -le 'print bgcd(10,90,40)'
10
- Calculate the greatest common divisor form a file or user input.
cat test.txt
10 90 40
perl -MMath::BigInt=bgcd -anle 'print bgcd(@F)' test.txt
10
- Calculate the least common multiple.
perl -MMath::BigInt=blcm -le 'print blcm(6,10,24)'
120
- Same as above but from either a file or user input.
cat test.txt
6 10 24
perl -MMath::BigInt=blcm -anle 'print blcm(@F)' test.txt
120
- Generate 8 random numbers between 2 and 20 (excluding 20).
perl -le 'print join ",", map { int(rand(20-2))+2 } 1..8'
10,6,3,19,11,10,8,6
- Generate all permutations of a list.
perl -MAlgorithm::Permute -le '$l = [1,2,3]; $p = Algorithm::Permute->new($l); print "@r" while @r = $p->next'
3 2 1
2 3 1
2 1 3
3 1 2
1 3 2
1 2 3
- Generate the powerset.
perl -MList::PowerSet=powerset -le '@l = (1,2,3); print "@$_" for @{powerset(@l)}'
1 2 3
2 3
1 3
3
1 2
2
1
- Convert an IP address to an unsigned integer.
perl -le '$i=3; $u += ($_<<8*$i--) for "127.0.0.1" =~ /(\d+)/g; print $u';
2130706433
- Convert an unsigned integer to an IP address
perl -MSocket -le 'print inet_ntoa(pack("N", 2130706433))'
127.0.0.1
- You can also do the above using the following method:
perl -le '$ip = 2130706433; print join ".", map { (($ip>>8*($_))&0xFF) } reverse 0..3';
127.0.0.1
- Generate and print the English alphabet.
perl -le 'print a..z'
abcdefghijklmnopqrstuvwxyz
- A more semantically correct version of the above.
perl -le 'print ("a".."z")'
abcdefghijklmnopqrstuvwxyz
- Print the English alphabet comma dilimited.
perl -le '$, = ","; print ("a".."z")'
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
- Generate and print all the strings from “a” to “zz”.
perl -le 'print join ",", ("a".."zz")'
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az,ba,bb,bc,bd,be,bf,bg,bh,bi,bj,bk,bl,bm,bn,bo,bp,bq,br,bs,bt,bu,bv,bw,bx,by,bz,ca,cb,cc,cd,ce,cf,cg,ch,ci,cj,ck,cl,cm,cn,co,cp,cq,cr,cs,ct,cu,cv,cw,cx,cy,cz,da,db,dc,dd,de,df,dg,dh,di,dj,dk,dl,dm,dn,do,dp,dq,dr,ds,dt,du,dv,dw,dx,dy,dz,ea,eb,ec,ed,ee,ef,eg,eh,ei,ej,ek,el,em,en,eo,ep,eq,er,es,et,eu,ev,ew,ex,ey,ez,fa,fb,fc,fd,fe,ff,fg,fh,fi,fj,fk,fl,fm,fn,fo,fp,fq,fr,fs,ft,fu,fv,fw,fx,fy,fz,ga,gb,gc,gd,ge,gf,gg,gh,gi,gj,gk,gl,gm,gn,go,gp,gq,gr,gs,gt,gu,gv,gw,gx,gy,gz,ha,hb,hc,hd,he,hf,hg,hh,hi,hj,hk,hl,hm,hn,ho,hp,hq,hr,hs,ht,hu,hv,hw,hx,hy,hz,ia,ib,ic,id,ie,if,ig,ih,ii,ij,ik,il,im,in,io,ip,iq,ir,is,it,iu,iv,iw,ix,iy,iz,ja,jb,jc,jd,je,jf,jg,jh,ji,jj,jk,jl,jm,jn,jo,jp,jq,jr,js,jt,ju,jv,jw,jx,jy,jz,ka,kb,kc,kd,ke,kf,kg,kh,ki,kj,kk,kl,km,kn,ko,kp,kq,kr,ks,kt,ku,kv,kw,kx,ky,kz,la,lb,lc,ld,le,lf,lg,lh,li,lj,lk,ll,lm,ln,lo,lp,lq,lr,ls,lt,lu,lv,lw,lx,ly,lz,ma,mb,mc,md,me,mf,mg,mh,mi,mj,mk,ml,mm,mn,mo,mp,mq,mr,ms,mt,mu,mv,mw,mx,my,mz,na,nb,nc,nd,ne,nf,ng,nh,ni,nj,nk,nl,nm,nn,no,np,nq,nr,ns,nt,nu,nv,nw,nx,ny,nz,oa,ob,oc,od,oe,of,og,oh,oi,oj,ok,ol,om,on,oo,op,oq,or,os,ot,ou,ov,ow,ox,oy,oz,pa,pb,pc,pd,pe,pf,pg,ph,pi,pj,pk,pl,pm,pn,po,pp,pq,pr,ps,pt,pu,pv,pw,px,py,pz,qa,qb,qc,qd,qe,qf,qg,qh,qi,qj,qk,ql,qm,qn,qo,qp,qq,qr,qs,qt,qu,qv,qw,qx,qy,qz,ra,rb,rc,rd,re,rf,rg,rh,ri,rj,rk,rl,rm,rn,ro,rp,rq,rr,rs,rt,ru,rv,rw,rx,ry,rz,sa,sb,sc,sd,se,sf,sg,sh,si,sj,sk,sl,sm,sn,so,sp,sq,sr,ss,st,su,sv,sw,sx,sy,sz,ta,tb,tc,td,te,tf,tg,th,ti,tj,tk,tl,tm,tn,to,tp,tq,tr,ts,tt,tu,tv,tw,tx,ty,tz,ua,ub,uc,ud,ue,uf,ug,uh,ui,uj,uk,ul,um,un,uo,up,uq,ur,us,ut,uu,uv,uw,ux,uy,uz,va,vb,vc,vd,ve,vf,vg,vh,vi,vj,vk,vl,vm,vn,vo,vp,vq,vr,vs,vt,vu,vv,vw,vx,vy,vz,wa,wb,wc,wd,we,wf,wg,wh,wi,wj,wk,wl,wm,wn,wo,wp,wq,wr,ws,wt,wu,wv,ww,wx,wy,wz,xa,xb,xc,xd,xe,xf,xg,xh,xi,xj,xk,xl,xm,xn,xo,xp,xq,xr,xs,xt,xu,xv,xw,xx,xy,xz,ya,yb,yc,yd,ye,yf,yg,yh,yi,yj,yk,yl,ym,yn,yo,yp,yq,yr,ys,yt,yu,yv,yw,yx,yy,yz,za,zb,zc,zd,ze,zf,zg,zh,zi,zj,zk,zl,zm,zn,zo,zp,zq,zr,zs,zt,zu,zv,zw,zx,zy,zz
- Convert the number 255 to HEX.
perl -le 'printf("%x", 255)';
ff
- Convert the HEX value of 255 back to decimal.
perl -le '$num = "ff"; print hex $num'
255
- Generate a random eight-character password.
perl -le 'print map { ("a".."z")[rand 26] } 1..8'
xdunnyta
- Same as above but also include numbers.
perl -le 'print map { ("a".."z", 0..9)[rand 36] } 1..8'
jed7ro3m
- Create a string of specific length ( 25 characters ).
perl -le 'print "a"x25'
aaaaaaaaaaaaaaaaaaaaaaaaa
- Based on the above use this command to generate 1KB of data.
perl -e 'print "a"x1024'
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- Create a list of 10 repetitions of ( 4, 6).
perl -le '@list = (4,6)x10; print "@list"'
4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6
- Create a string from command-line arguments.
perl -le 'print "(", (join ",", @ARGV), ")"' foo bar baz
(foo,bar,baz)
- Find the numeric values for characters in a string.
perl -le 'print join ", ", map { ord } split //, "penguins are cool"'
112, 101, 110, 103, 117, 105, 110, 115, 32, 97, 114, 101, 32, 99, 111, 111, 108
- Convert a list of numeric ASCII values into a string.
perl -le '@ascii = (76, 73, 78, 85, 88); print pack("C*", @ascii)';
LINUX
- Generate an array with odd numbers from 10 to 50.
perl -le '@odd = grep {$_ % 2 == 1} 10..50; print "@odd"'
11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
- Generate an array with even numbers from 10 90.
perl -le '@even = grep {$_ % 2 == 0} 10..90; print "@even"'
10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90
- Find the length of a string.
perl -le 'print length "Penguins are cool!"'
18
- Find the number of elements in an array.
perl -le '@array = ("a".."z"); print scalar @array'
26
- Same as above but adding 1 to the last index of an array.
perl -le '@array = ("a".."z"); print $#array + 1'
26
- Print the number of files in a directory with the .txt file extension.
ls
bar.txt baz.pdf foo.txt
perl -le 'print scalar @ARGV' *.txt
2
- Rot13 the string 'penguin'.
perl -le '$string = "penguin"; $string =~ y/A-Za-z/N-ZA-Mn-za-m/; print $string'
crathva
- Base64-encode the string 'penguin'.
perl -MMIME::Base64 -e 'print encode_base64("penguin")'
cGVuZ3Vpbg==
- Base64-encode the contents of a file.
cat foo.txt
penguin
perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' foo.txt
cGVuZ3Vpbgo=
- Base64-decode a string.
perl -MMIME::Base64 -le 'print decode_base64("cGVuZ3Vpbgo=")'
penguin
- Base64-decode the contents of a file.
cat foo.txt
cGVuZ3Vpbgo=
perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)' foo.txt
penguin
- URL-escape a string.
perl -MURI::Escape -le 'print uri_escape("https://penguins.com")'
https%3A%2F%2Fpenguins.com
- URL-unescape a string.
perl -MURI::Escape -le 'print uri_unescape("https%3A%2F%2Fpenguins.com")'
https://penguins.com
- HTML-encode a string.
perl -MHTML::Entities -le 'print encode_entities("<html>")'
<html>
- HTML-decode a string.
perl -MHTML::Entities -le 'print decode_entities("<html>")'
<html>
- Convert all text to uppercase.
echo 'penguins' |perl -nle 'print uc'
PENGUINS
- Convert all text in a file to uppercase.
cat foo.txt
penguins
linux
bsd
perl -nle 'print uc' foo.txt
PENGUINS
LINUX
BSD
- Same as above using $_ instead.
perl -ple '$_ = uc' foo.txt
PENGUINS
LINUX
BSD
- Convert all text to lowercase.
echo 'PENGUINS' |perl -nle 'print lc'
penguins
- Uppercase only the first letter of each line.
cat foo.txt
penguins
linux
bsd
perl -nle 'print ucfirst lc' foo.txt
Penguins
Linux
Bsd
- Invert the letter case.
cat foo.txt
Penguins
Linux
BSD
perl -ple 'y/A-Za-z/a-zA-Z/' foo.txt
pENGUINS
lINUX
bsd
- Title-case each line.
cat foo.txt
penguins are cool.
penguins are cool and fluffy.
BSD devils are awesome.
perl -ple 's/(\w+)/\u$1/g' foo.txt
Penguins Are Cool.
Penguins Are Cool And Fluffy.
BSD Devils Are Awesome.
- Strip leading whitespace (e.g. spaces, tabs) from the beginning of each line.
cat foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
perl -ple 's/^[ \t]+//' foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
- Same as above but replace the regular expression with a \s+.
cat foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
perl -ple 's/^\s+//' foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
- Strip trailing whitespace (e.g. spaces, tabs) from the end of each line.
perl -ple 's/[ \t]+$//' foo.txt
- Same as above but replace the regular expression with a \s+.
perl -ple 's/\s+$//' foo.txt
- Strip whitespace (e.g. spaces, tabs) from the beginning and end of each line.
cat foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
perl -ple 's/^[ \t]+|[ \t]+$//g' foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
- Same as above but replace the regular expression with a \s+.
perl -ple 's/^\s+|\s+$//g' foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
- Convert UNIX newlines to DOS/Windows newlines.
cat foo.txt
Linux is cool!
Penguins are cool too!
OpenBSD is pretty secure!
cat foo.txt |xxd
00000000: 4c69 6e75 7820 6973 2063 6f6f 6c21 0a50 Linux is cool!.P
00000010: 656e 6775 696e 7320 6172 6520 636f 6f6c enguins are cool
00000020: 2074 6f6f 210a 4f70 656e 4253 4420 6973 too!.OpenBSD is
00000030: 2070 7265 7474 7920 7365 6375 7265 210a pretty secure!.
perl -pe 's|\015\012|\012|' foo.txt > bar.txt
cat bar.txt |xxd
00000000: 4c69 6e75 7820 6973 2063 6f6f 6c21 0d0a Linux is cool!..
00000010: 5065 6e67 7569 6e73 2061 7265 2063 6f6f Penguins are coo
00000020: 6c20 746f 6f21 0d0a 4f70 656e 4253 4420 l too!..OpenBSD
00000030: 6973 2070 7265 7474 7920 7365 6375 7265 is pretty secure
00000040: 210d 0a !..
-
Convert DOS/Windows newlines to UNIX newlines. Same as above but use perl -pe 's|\015\012|\012|' instead.
-
Convert UNIX newlines to Mac newlines. Same as above but use perl -pe 's|\012|\015|' instead.
-
Substitute (e.g. search and replace) “cool” with “awesome” on each line.
cat foo.txt
Linux is cool! Linus is also cool!
Penguins are cool too!
perl -pe 's/cool/awesome/' foo.txt
Linux is awesome! Linus is also cool!
Penguins are awesome too!
Notice this only repalces the first occurance of cool with awesome in each line.
- Same as above but substitue ALL occurances ( e.g. global search & replace) by using the /g flag.
perl -pe 's/cool/awesome/g' foo.txt
Linux is awesome! Linus is also awesome!
Penguins are awesome too!
- Substitute (e.g search and replace) “cool” with “awesome” on lines that match “definitley”.
cat foo.txt
Linux is cool! Definitely!
Penguins are cool too! Definitely!
M$ is also cool!
perl -pe '/Definitely/ && s/cool/awesome/' foo.txt
Linux is awesome! Definitely!
Penguins are awesome too! Definitely!
M$ is also cool!
- Print paragraphs in reverse order.
cat foo.txt
Once upon a time in
in a galaxy far far away there lived
a penguin & his name was TUX.
One day TUX was taking a cool
dip in the Gulf of Bothnia when
he met an interesting wildebeest
named GNU.
perl -00 -e 'print reverse <>' foo.txt
One day TUX was taking a cool
dip in the Gulf of Bothnia when
he met an interesting wildebeest
named GNU.
Once upon a time in
in a galaxy far far away there lived
a penguin & his name was TUX.
- Print all lines in reverse order.
cat foo.txt
Linux is cool!
Penguins are also cool!
perl -lne 'print scalar reverse $_' foo.txt
!looc si xuniL
!looc osla era sniugneP
- Same as above but dropping special variable _$__.
perl -lne 'print scalar reverse' foo.txt
!looc si xuniL
!looc osla era sniugneP
- Print columns in reverse order.
cat foo.txt
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
perl -alne 'print "@{[reverse @F]}"' foo.txt
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
- Same as above but when your columns are seperated by a delimiter such as a ,.
cat foo.txt
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
perl -F, -alne 'print "@{[reverse @F]}"' foo.txt
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
- The same as above but include your , delimiter.
perl -F, -alne '$" = ","; print "@{[reverse @F]}"' foo.txt
5,4,3,2,1
10,9,8,7,6
15,14,13,12,11
- Print the first line of a file (e.g. emulate head -1).
cat foo.txt
Linux is cool!
Penguins are also cool!
perl -ne 'print; exit' foo.txt
Linux is cool!
- Same as above but actually alter the contents of foo.txt.
perl -i -ne 'print; exit' foo.txt
cat foo.txt
Linux is cool!
- Same as above but make a backup file before altering the contents of foo.txt.
cat foo.txt
Linux is cool!
Penguins are also cool!
perl -i.bak -ne 'print; exit' foo.txt
cat foo.txt
Linux is cool!
cat foo.txt.bak
Linux is cool!
Penguins are also cool!
Notice the original contents of foo.txt are the same as foo.txt.bak.
- Print the first 2 lines of a file (e.g. emulate head -2).
cat foo.txt
Linux is cool!
Penguins are also cool!
BSD devil is also cool!
perl -ne 'print if $. <= 2' foo.txt
Linux is cool!
Penguins are also cool!
- Same as above but without the if statement.
perl -ne '$. <= 2 && print' foo.txt
Linux is cool!
Penguins are also cool!
- Same as above but using the range operator.
perl -ne 'print if 1..2' foo.txt
Linux is cool!
Penguins are also cool!
- Same as above put print based on a condition.
perl -ne 'print; exit if $. == 2' foo.txt
Linux is cool!
Penguins are also cool!
- Print the last line of a file (e.g. emulate tail -1).
cat foo.txt
Linux is cool!
Penguins are also cool!
BSD devil is also cool!
perl -ne '$last = $_; END { print $last }' foo.txt
BSD devil is also cool!
- Same as above but using the EOF ( End of File) function instead.
perl -ne 'print if eof' foo.txt
BSD devil is also cool!
- Print the last 2 lines of a file (e.g. emulate tail -2).
perl -ne 'push @a, $_; @a = @a[@a-2..$#a] if @a>2; END { print @a }' foo.txt
Penguins are also cool!
BSD devil is also cool!
- Same as above but a simpler version using shift instead.
perl -ne 'push @a, $_; shift @a if @a>2; END { print @a }' foo.txt
Penguins are also cool!
BSD devil is also cool!
- Print only lines that match a regular expression.
cat foo.txt
Linux is cool!
Penguins are also cool!
BSD devil is also cool!
Penguins are cute!
perl -ne '/Penguins/ && print' foo.txt
Penguins are also cool!
Penguins are cute!
- Same as above but using print if.
perl -ne 'print if /Penguins/' foo.txt
Penguins are also cool!
Penguins are cute!
- Print only lines that do not match a regular expression.
cat foo.txt
Linux is cool!
Penguins are also cool!
BSD devil is also cool!
Penguins are cute!
perl -ne '!/Penguins/ && print' foo.txt
Linux is cool!
BSD devil is also cool!
- Same as above using print if.
perl -ne 'print if !/Penguins/' foo.txt
Linux is cool!
BSD devil is also cool!
- Same as above using print unless instead.
perl -ne 'print unless /Penguins/' foo.txt
Linux is cool!
BSD devil is also cool!
- Print every line preceding a line that matches a regular expression.
cat foo.txt
Linux is cool!
Penguins are also cool!
BSD devil is also cool!
Penguins are cute!
perl -ne '/Penguins/ && $last && print $last; $last = $_' foo.txt
Linux is cool!
BSD devil is also cool!
- Print every line following a line that matches a regular expression.
cat foo.txt
Linux is cool!
Penguins are cool!
Linux is awesome!
Penguins are cute!
perl -ne 'if ($p) { print; $p = 0 } $p++ if /Linux/' foo.txt
Penguins are cool!
Penguins are cute!
- Same as above using && instead of using if and curly brackets.
perl -ne '$p && print && ($p = 0); $p++ if /Linux/' foo.txt
Penguins are cool!
Penguins are cute!
- Same as above but much more simplified.
perl -ne '$p && print; $p = /Linux/' foo.txt
Penguins are cool!
Penguins are cute!
- Print lines that match regular expressions Linux and cool in any order.
cat foo.txt
Linux is cool!
Penguins are cool!
OpenBSD is secure!
FreeBSD is awesome!
Linux is cool!
Penguins are cute!
perl -ne '/Linux/ && /cool/ && print' foo.txt
Linux is cool!
Linux is cool!
- Print lines that don't match regular expressions Linux and cool in any order.
cat foo.txt
Linux is cool!
Penguins are cool!
OpenBSD is secure!
FreeBSD is awesome!
Linux is awesome!
Penguins are cute!
perl -ne '!/Linux/ && !/cool/ && print' foo.txt
OpenBSD is secure!
FreeBSD is awesome!
Penguins are cute!
- Print lines that match regular expression 111 followed by AAA followed by 222.
cat foo.txt
Linux is cool!
Penguins are cool!
111 AAA 222
OpenBSD is secure!
FreeBSD is awesome!
111 AAA 222
Linux is cool!
Penguins are cute!
perl -ne '/111.*AAA.*222/ && print' foo.txt
111 AAA 222
111 AAA 222
- Print lines that are at least 50 characters long.
cat foo.txt
Linux is cool!
Penguins are cool!
111 AAA 222
OpenBSD is secure!
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
FreeBSD is awesome!
111 AAA 222
Linux is cool!
Penguins are cute!
perl -ne 'print if length >= 50' foo.txt
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
- Print lines that are fewer than 14 characters long.
perl -ne 'print if length() < 14' foo.txt
111 AAA 222
111 AAA 222
- Print only line 4.
cat foo.txt
Linux is cool!
Penguins are cool!
111 AAA 222
OpenBSD is secure!
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
FreeBSD is awesome!
111 AAA 222
Linux is cool!
Penguins are cute!
perl -ne '$. == 4 && print && exit' foo.txt
OpenBSD is secure!
- Print all lines except line 4.
perl -ne '$. != 4 && print' foo.txt
Linux is cool!
Penguins are cool!
111 AAA 222
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
FreeBSD is awesome!
111 AAA 222
Linux is cool!
Penguins are cute!
- Same as above but slightly re-factored.
perl -ne 'print if $. != 4' foo.txt
- Same as above but using the unless function.
perl -ne 'print unless $. == 4' foo.txt
- Print only lines 1, 5 & 8.
cat foo.txt
Linux is cool!
Penguins are cool!
111 AAA 222
OpenBSD is secure!
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
FreeBSD is awesome!
111 AAA 222
Linux is cool!
Penguins are cute!
perl -ne 'print if $. == 1 || $. == 5 || $. == 8' foo.txt
Linux is cool!
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
Linux is cool!
- If you want to print several lines you can add them to an array.
perl -ne '@lines = (1, 2, 4, 6, 8, 9); print if grep { $_ == $. } @lines' foo.txt
Linux is cool!
Penguins are cool!
OpenBSD is secure!
FreeBSD is awesome!
Linux is cool!
Penguins are cute!
- Print all lines from 4 through 6.
perl -ne 'print if $. >= 4 && $. <= 6' foo.txt
OpenBSD is secure!
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
FreeBSD is awesome!
- Same as above but using the flip-flop operator.
perl -ne 'print if 4..6' foo.txt
OpenBSD is secure!
al;ksfjaslkdfjaslkdjfasl;kdfjaslkjdfasl;kdjfasl;dkfjasldkf
FreeBSD is awesome!
- Print all lines between two regular expressions (including the lines that match).
cat foo2.txt
one Linux is cool!
Linux is awesome!
Penguins are cute! two
OpenBSD is secure!
perl -ne 'print if /one/../two/' foo2.txt
one Linux is cool!
Linux is awesome!
Penguins are cute! two
- Print the longest line. Note: also include -l if you want to prevent the newline characters from counting toward the line length.
cat foo2.txt
one Linux is cool!
Linux is awesome!
al;skdfawo;iefjlskdna;okewfa;lskdfal;skndf
Penguins are cute! two
OpenBSD is secure!
perl -ne '$l = $_ if length($_) > length($l); END { print $l }' foo2.txt
al;skdfawo;iefjlskdna;okewfa;lskdfal;skndf
- Print the shortest line.
perl -ne '$s = $_ if $. == 1; $s = $_ if length($_) < length($s); END { print $s }' foo2.txt
Linux is awesome!
- Print all lines containing digits.
cat foo2.txt
1 Linux is cool!
Linux is awesome!
al;skdfawo;iefjlskdna;okewfa;lskdfal;skndf
Penguins are cute! 2
OpenBSD is secure!
perl -ne 'print if /\d/' foo2.txt
1 Linux is cool!
Penguins are cute! 2
- Print all lines containing only digits.
cat foo2.txt
Linux is cool!
Linux is awesome!
124569806598476
Penguins are cute!
OpenBSD is secure!
perl -ne 'print if /^\d+$/' foo2.txt
124569806598476
- Same as above using the unless function & the \D regular expression.
perl -lne 'print unless /\D/' foo2.txt
124569806598476
- Print all lines containing only alphabetic characters.
cat foo2.txt
Linux is cool!
LinuxIsAwesome
124569806598476
Penguins are cute!
OpenBSD is secure!
perl -ne 'print if /^[[:alpha:]]+$/' foo2.txt
LinuxIsAwesome
- Print every second line beginning with the first line.
cat foo2.txt
Linux is cool!
Linux is awesome!
124569806598476
Penguins are cute!
OpenBSD is secure!
Penguins are cool!
perl -ne 'print if $. % 2' foo2.txt
Linux is cool!
124569806598476
OpenBSD is secure!
- Print every second line, beginning with the second line.
perl -ne 'print if $. % 2 == 0' foo2.txt
Linux is awesome!
Penguins are cute!
Penguins are cool!
- Same as above but invert the test from previous example.
perl -ne 'print unless $. % 2' foo2.txt
Linux is awesome!
Penguins are cute!
Penguins are cool!
- Print all repeated lines only once.
cat foo2.txt
Linux is cool!
Linux is cool!
124569806598476
Penguins are cute!
OpenBSD is secure!
Penguins are cute!
perl -ne 'print if ++$a{$_} == 2' foo2.txt
Linux is cool!
Penguins are cute!
- Print all unique lines.
perl -ne 'print unless $a{$_}++' foo2.txt
Linux is cool!
124569806598476
Penguins are cute!
OpenBSD is secure!
- Print lines that look like an Ip address but not necessairly a valid Ip address.
cat foo2.txt
127.0.0.1
1.2.4.5
999.45.78.12
192.168.1.1
foo127.0.0.bar1
perl -ne 'print if /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/' foo2.txt
127.0.0.1
1.2.4.5
999.45.78.12
192.168.1.1
- Confirm a regular expression really matches all numbers in the range 0 to 255.
perl -le 'map { $n++ if /^([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/ } 0..255; END { print $n }'
256
- Confirm the above one-liner doesn’t match numbers above 255.
perl -le 'map { $n++ if /^([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/ } 0..500; END { print $n }'
256
If this regular expression would have matched numbers above 255 the output would not have been 256.
- Match a valid Ip address by saving our regular expression to a variable.
cat foo2.txt
127.0.0.1
1.2.4.5
999.45.78.12
192.168.1.1
foo127.0.0.bar1
perl -ne '$ip_part = qr{([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])}; print if /^($ip_part\.){3}$ip_part$/' foo2.txt
127.0.0.1
1.2.4.5
192.168.1.1
- Check if a string looks like an email address.
cat foo2.txt
[email protected]
[email protected]
[email protected]
1245
dkjsond@sdkfj
perl -ne 'print if /\S+@\S+\.\S+/' foo2.txt
[email protected]
[email protected]
[email protected]
- A more robust way of identifying an email address is by using the CPAN module Email::Valid.
perl -MEmail::Valid -ne 'print if Email::Valid->address($_)' foo2.txt
[email protected]
[email protected]
[email protected]
- Match printable ASCII characters.
cat foo2.txt
1
2
10
5
100
perl -ne 'print if /[ -~]/' foo2.txt
1
2
10
5
100