Sponsored Content
Full Discussion: Pattern Matching in UNIX
Top Forums Shell Programming and Scripting Pattern Matching in UNIX Post 302195235 by era on Wednesday 14th of May 2008 03:07:05 PM
Old 05-14-2008
You need to properly quote variables with spaces or other special characters in them.

If you want to use Perl, it's rather weird to not do the actual matching in Perl also. Running the grep twice just because you want the number of matches first is also ... intriguing. Here, I cannot resist.

Code:
#!/usr/bin/perl
print "Enter a File name :";
chomp ($file = <STDIN>);
print "\n Searching file :";
if (-e $file)
{
    print "File Found\n";

    $lines = `wc -l < $file`;
    chomp $lines;

    print "Total number of lines in the file = $lines \n";

    print "Enter the pattern to search :";
    chomp ($pattern = <STDIN>);
    print "\n";
    # to search the no of words (pattern search)
    $abc=`grep "$pattern" $file`;
    $count = () = $abc =~ m/\n/g;
    print "Total number of results found: $count\n";
    print "here are the results ...\n$abc\n";
}
else{
    print "File not Found\n";
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern Matching problem in UNIX

Hello All, I need help I have a problem in searching the pattern in a file let us say the file contains the below lines line 1 USING *'/FILE/FOLDER/RETURN') ................. ................. line 4 USING *'/FILE/FOLDER/6kdat1') line 5 USING... (2 Replies)
Discussion started by: maxmave
2 Replies

2. UNIX for Advanced & Expert Users

pattern matching in unix

Task is to identify files like code.1 , code.23 and so on ... (the files which are ending with a number) but it should not match files like code.123abc. So the search will normally search for files with "code." and at the end we should extract for the correct match. Now I have to remove these files... (6 Replies)
Discussion started by: diwakar_reddy
6 Replies

3. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

4. Shell Programming and Scripting

Perl Pattern Matching:Unix Vs. Window (Is it OS dependent??)

Hi All, Below I am discussing the problem I am facing while using pattern matching in Unix & Window. Plz hv a look into it. ================ my $s="UPDATE A SET s="klkkk' ;" ; if ( $s =~ m/^*UPDATE+/i ) { print $s; } else { print "no match";} =================== Both should... (6 Replies)
Discussion started by: Niroj
6 Replies

5. Shell Programming and Scripting

Issue with Pattern Matching in Unix

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK,CARE_OF 1,Robert,74538,12,RICHARD JOHNSON, P.C 2,Sam,07564,13,% R.S MIKE, V.K.S 3,Kim, Ed,12345,14,@90 KMS, %TK Desired Output: SNO,NAME,ZIPCODE,RANK,CARE_OF... (1 Reply)
Discussion started by: techmoris
1 Replies

6. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

7. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

8. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

9. UNIX for Beginners Questions & Answers

Binary pattern matching in UNIX

I think what I'm trying to do is pretty straightforward but I just can't find a way to do it. I'm trying to run a double pattern match in a three column file. If the first two columns match, I need to output the third. So in the file AAA BBB 1 BBC CCC 5 CCC DDD 7 DDD EEE 12 If the... (4 Replies)
Discussion started by: matthewndavies1
4 Replies

10. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies
PERLTW(1)						 Perl Programmers Reference Guide						 PERLTW(1)

XXXXXXXXXXXXXXXXXX, XXXXXXXXXXXX.  XXXXXX POD (XXXXXX) XX; XXXXXXXXXXXXXX, XXXXXX. XXXXXXXXXXX, XXX perlpod XXXX.

NAME
perltw - XXXX Perl XX DESCRIPTION
XXXX Perl XXX! X 5.8.0 XXX, Perl XXXXXX Unicode (XXX) XX, XXXXXXXXXXXXXXXXXXX; CJK (XXX) XXXXXXXX. Unicode XXXXXXX, XXXXXXXXXXXX: XXXX, XXXX, XXXXXXXX (XXX, XXXX, XXXX, XXXX, XXX, XXXX, XX). XXXXXXXXXXXXXX (X PC XXXX). Perl XXX Unicode XXXX. XXX Perl XXXXXXXXX Unicode XX; Perl XXXXXX (XXXXXXXXX) XXX Unicode XXXX. XXXXXXX, XXXXX Unicode XXXXXXXXXXXX, Perl XXX Encode XXXX, XXXXXXXXXXXXXXXXXXX. Encode XXXXXXXXXXXXXXXXX ('big5' XX 'big5-eten'): big5-eten Big5 XX (XXXXXXX) big5-hkscs Big5 + XXXXX, 2001 XX cp950 XXX 950 (Big5 + XXXXXXX) XXXX, X Big5 XXXXXXX Unicode, XXXXXXXX: perl -Mencoding=big5,STDOUT,utf8 -pe1 < file.big5 > file.utf8 Perl XXXX "piconv", XXXXX Perl XXXXXXXXXXX, XXXX: piconv -f big5 -t utf8 < file.big5 > file.utf8 piconv -f utf8 -t big5 < file.utf8 > file.big5 XX, XX encoding XX, XXXXXXXXXXXXXXXXX, XXXX: #!/usr/bin/env perl # XX big5 XXXX; XXXXXXXXXXXXX big5 XX use encoding 'big5', STDIN => 'big5', STDOUT => 'big5'; print length("XX"); # 2 (XXXXXXX) print length('XX'); # 4 (XXXXXXXX) print index("XXXX", "XX"); # -1 (XXXXXXX) print index('XXXX', 'XX'); # 1 (XXXXXXXXX) XXXXXXXX, "X" XXXXXXXX "X" XXXXXXXXXX Big5 XX "X"; "X" XXXXXXXXX "X" XXXXXXXXXX "X". XXXXXX Big5 XXXXXXXXXXX. XXXXXXX XXXXXXXXXXX, XXX CPAN (<http://www.cpan.org/>) XX Encode::HanExtra XX. XXXXXXXXXXX: cccii 1980 XXXXXXXXXXXX euc-tw Unix XXXXX, XX CNS11643 XX 1-7 big5plus XXXXXXXXXXXXX Big5+ big5ext XXXXXXXXXXXXX Big5e XX, Encode::HanConvert XXXXXXXXXXXXXXXX: big5-simp Big5 XXXXX Unicode XXXXXX gbk-trad GBK XXXXX Unicode XXXXXX XXX GBK X Big5 XXXX, XXXXXXXXX b2g.pl X g2b.pl XXXX, XXXXXXXXXXX: use Encode::HanConvert; $euc_cn = big5_to_gb($big5); # X Big5 XX GBK $big5 = gb_to_big5($euc_cn); # X GBK XX Big5 XXXXXX XXX Perl XXXXXXXXX (XXXXXXXXX), XXXXXXX Perl XXX, XX Unicode XXXXX. XX, XXXXXXXXX: XX Perl XXXXX <http://www.perl.com/> Perl XXX (XXXXXXXX) <http://www.cpan.org/> Perl XXXXX (Comprehensive Perl Archive Network) <http://lists.perl.org/> Perl XXXXXX XX Perl XXX <http://www.oreilly.com.tw/product_perl.php?id=index_perl> XXXXXXXXX Perl XX <http://groups.google.com/groups?q=tw.bbs.comp.lang.perl> XX Perl XXXXX (XXXXX BBS X Perl XXX) Perl XXXXX <http://www.pm.org/groups/taiwan.html> XX Perl XXXXX <irc://irc.freenode.org/#perl.tw> Perl.tw XXXXX Unicode XXXX <http://www.unicode.org/> Unicode XXXX (Unicode XXXXXX) <http://www.cl.cam.ac.uk/%7Emgk25/unicode.html> Unix/Linux XX UTF-8 X Unicode XXX XXXXX XXXXXXX <http://www.cpatch.org/> Linux XXXXXXX <http://www.linux.org.tw/CLDP/> SEE ALSO
Encode, Encode::TW, encoding, perluniintro, perlunicode AUTHORS
Jarkko Hietaniemi <jhi@iki.fi> Audrey Tang (XX) <audreyt@audreyt.org> perl v5.18.2 2014-01-06 PERLTW(1)
All times are GMT -4. The time now is 01:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy