faqts : Computers : Programming : Languages : Perl

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 28 people (32%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

Perl: Search: File: How to search for a regular expression in a file from command line?

Mar 28th, 2005 06:11
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 25 December 2004 - 04:43 am -------------------

Perl: Search: File: How to search for a regular expression in a file 
from command line?

---

Use the '<>' diamond operator.

This operator gets its data from the file(s) you specified on the
command line.

You then check line after line for this regular expression

---
---

Steps: Overview:

 1. -Create a Perl file (e.g. 'test.pl') containing text
     similar to the following
     (in this example it shows all lines containing the
      word 'http:', make this variable using e.g. the @ARGV array,
      so that you can input it from the command line also)

--- cut here: begin --------------------------------------------------

while( <> ) {
 if ( $_ =~ /http:/ ) {
  print $_;
 }
}

--- cut here: end ----------------------------------------------------

 2. -On the MSDos command line type the command

      perl <your perl filename> <your text file1> <your text file 
2> ... <your text file last>

     e.g.

      perl test.pl ddd1.txt ddd2.txt

      shows the line

       'http://www.test.com'

 3. -The script will then print all lines in all this text files
     that contain this regular expression.

---
---

Internet: see also:

---

Perl: File: Line: Read: All: How to read all lines in given text file
(s)? [command line]
http://www.faqts.com/knowledge_base/view.phtml/aid/32881/fid/200

---

Perl: Command: Line: Parameter: How to print all command line 
parameters?
http://www.faqts.com/knowledge_base/view.phtml/aid/32887/fid/200

---

Search/Replace: Regular Expression: Links: Can you give an overview of 
links?
http://www.faqts.com/knowledge_base/view.phtml/aid/34985/fid/828

----------------------------------------------------------------------