perl script to search n place a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script to search n place a pattern
# 1  
Old 11-10-2011
perl script to search n place a pattern

hi,
i have one file as t1.txt as below
hi
hello
welcome

i want perl script to search for the pattern "abcd" in the file.
if the pattern doesn't exist, i want to insert that pattern at the end of the same file t1.txt
my o/p should be

hi
hllo
welcome
abcd

thank you
# 2  
Old 11-10-2011
anything you tried so far ?
# 3  
Old 11-10-2011
Quick code, but shell script

Code:
$ if [ grep -q 'abcd' a.txt ]; then 
<DO SOMETHING / LEAVE IT BLANK>
else
  echo "abcd" >> a.txt
fi

* in perl code, you would read a file in an array
* Use perl - grep function to check if abcd exist?
* if yes do what even you want
* if it does not, open another file in write mode add "abcd" to end of old array ( $Array[-1] )
* print it on new file handler
* move new_file -> old_file
# 4  
Old 11-10-2011
Code:
 
$ cat test
hi
hello
welcome
 
$ perl -lane 'print $_; $a=1 if $_=~/abcd/; END{if ($a ne 1) {print "abcd"}}' test
hi
hello
welcome
abcd
 
$ echo "abcd" >> test
 
$ cat test
hi
hello
welcome
abcd
 
$ perl -lane 'print $_; $a=1 if $_=~/abcd/; END{if ($a ne 1) {print "abcd"}}' test
hi
hello
welcome
abcd

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. UNIX for Beginners Questions & Answers

Using forward slash in search pattern in perl script

I have existing pattern in the perl script as: my $pattern = "^Line.*?:|^Errors*: |^SEVERE:.*?:|^Null pointer exception occurred"; and I wanted to include below keywords in my search pattern "I/O exception" and "FileNotFoundException"the problem is when I include my pattern like my... (5 Replies)
Discussion started by: ambarginni
5 Replies

3. Shell Programming and Scripting

Using Shell Script in place of Perl script to Unzip the zip files.

Hi Expert, We have some shell scripts which Internally uses Perl Script to Unzip the source zip files which comes to inbound directory. So now our requirement is to avoid the dependency on Perl Script and us Shell Script to unzip the files. I have the Perl script with me attached can some one... (3 Replies)
Discussion started by: naveen.dasu
3 Replies

4. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

5. Shell Programming and Scripting

Multiple pattern search in perl

user 10 values content is: musage.py yes value user 11 values content is: gusage.py yes value how to print "user" string line by searching "content is:" string and "usage.py" string in perl (8 Replies)
Discussion started by: Anjan1
8 Replies

6. Shell Programming and Scripting

search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files. After the hex error code is found, the occurences would be counted. Afterwards the found hex errorcode would be cat into a separate file. Here is my code: #!/usr/bin/perl use File::Basename; my $find = $ARGV; my... (2 Replies)
Discussion started by: sdohn
2 Replies

7. Shell Programming and Scripting

perl pattern search

can someone help me out with the bolded? else if (regmatch($Subject, "^Application") && (regmatch($From, "^etgh") && (regmatch($Body, ".*not authorized to use this server.*")))) what this section of the code is suppose to do is to scan through the contents of $Body, if do a set of... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

9. Shell Programming and Scripting

Perl Search pattern error

Hi, I am trying to grep for two patterns from a set of 820 apache webserver logs, When I Try to search the pattern with a date stamp and use a wildcard character * for ex: /28/Aug/2008:21*/ to get all the log entries for that particular hour that is 21 st hour I get errors. Please kindly... (1 Reply)
Discussion started by: openspark
1 Replies

10. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies
Login or Register to Ask a Question