Sponsored Content
Top Forums Shell Programming and Scripting Perl: Search for string on line then search and replace text Post 302155300 by Crypto on Thursday 3rd of January 2008 01:20:50 PM
Old 01-03-2008
Perl: Search for string on line then search and replace text

Hi All,

I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text.

An example of 4 lines in my file is:

1. MatchText_randomNumberOfText moreData ReplaceMe moreData
2. MatchText_randomNumberOfText moreData moreData ReplaceMe moreData moreData
3. MatchText_randomNumberOfText moreData ReplaceMe moreData moreData
4. TextTextText_randomNumberOfText moreData ReplaceMe moreData moreData

The above is an example of four lines. I want to find all the lines containing "MatchText" (lines 1,2 and 3) and replace the "ReplaceMe" in that line with "REPLACED".

"ReplaceMe" occurs multiple times in lines I do not want to replace it in (as in line 4 above)

Hope that makes sense!!
Many thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Search and replace entire line

I have a perl function in my script that needs to replace an entire line in a file sub changestate { my $base = (); my @base = (); open(BASE, $file) || die("Could not open file!"); @base=<BASE>; close (BASE); foreach $base(@base) { if($base =~... (1 Reply)
Discussion started by: insania
1 Replies

2. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

3. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

4. Shell Programming and Scripting

Search a string,get line and replace with second field

Hi, I need to search for source path in file2 , as per file1 and if found get the next line and take the field value and put it in URL value of file1. In file1, NF is not same for all the lines. file1: <type source="/home/USER/Desktop" Dest="/home/USER/DIR1/Desktop" URL="ssh/path"/> <type... (8 Replies)
Discussion started by: greet_sed
8 Replies

5. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

6. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

7. UNIX for Advanced & Expert Users

Search and replace a line in perl

Hi All, i can replace a perticular value in sentence using perl. perl -pi -e 's/old/new/' sample.txt but i am not able to replace whole string by perl. file1 contains "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DATA=(SID= MWDBD22)))". i... (3 Replies)
Discussion started by: arindam guha
3 Replies

8. Shell Programming and Scripting

Search a string in a text and replace

i am having text file below System will display value URGENT and proceed System will display value URGENT and proceed System will display value URGENT and proceed .................................................................. (1 Reply)
Discussion started by: suryanarayana
1 Replies

9. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

10. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies
CIDR_TABLE(5)							File Formats Manual						     CIDR_TABLE(5)

NAME
cidr_table - format of Postfix CIDR tables SYNOPSIS
postmap -q "string" cidr:/etc/postfix/filename postmap -q - cidr:/etc/postfix/filename <inputfile DESCRIPTION
The Postfix mail system uses optional lookup tables. These tables are usually in dbm or db format. Alternatively, lookup tables can be specified in CIDR (Classless Inter-Domain Routing) form. In this case, each input is compared against a list of patterns. When a match is found, the corresponding result is returned and the search is terminated. To find out what types of lookup tables your Postfix system supports use the "postconf -m" command. To test lookup tables, use the "postmap -q" command as described in the SYNOPSIS above. TABLE FORMAT
The general form of a Postfix CIDR table is: pattern result When a search string matches the specified pattern, use the corresponding result value. The pattern must be in network/prefix or network_address form (see ADDRESS PATTERN SYNTAX below). !pattern result When a search string does not match the specified pattern, use the specified result value. The pattern must be in network/prefix or network_address form (see ADDRESS PATTERN SYNTAX below). This feature is available in Postfix 3.2 and later. if pattern endif When a search string matches the specified pattern, match that search string against the patterns between if and endif. The pattern must be in network/prefix or network_address form (see ADDRESS PATTERN SYNTAX below). The if..endif can nest. Note: do not prepend whitespace to text between if..endif. This feature is available in Postfix 3.2 and later. if !pattern endif When a search string does not match the specified pattern, match that search string against the patterns between if and endif. The pattern must be in network/prefix or network_address form (see ADDRESS PATTERN SYNTAX below). The if..endif can nest. Note: do not prepend whitespace to text between if..endif. This feature is available in Postfix 3.2 and later. blank lines and comments Empty lines and whitespace-only lines are ignored, as are lines whose first non-whitespace character is a `#'. multi-line text A logical line starts with non-whitespace text. A line that starts with whitespace continues a logical line. TABLE SEARCH ORDER
Patterns are applied in the order as specified in the table, until a pattern is found that matches the search string. ADDRESS PATTERN SYNTAX
Postfix CIDR tables are pattern-based. A pattern is either a network_address which requires an exact match, or a network_address/pre- fix_length where the prefix_length part specifies the length of the network_address prefix that must be matched (the other bits in the net- work_address part must be zero). An IPv4 network address is a sequence of four decimal octets separated by ".", and an IPv6 network address is a sequence of three to eight hexadecimal octet pairs separated by ":" or "::", where the latter is short-hand for a sequence of one or more all-zero octet pairs. The pattern 0.0.0.0/0 matches every IPv4 address, and ::/0 matches every IPv6 address. IPv6 support is available in Postfix 2.2 and later. Before comparisons are made, lookup keys and table entries are converted from string to binary. Therefore, IPv6 patterns will be matched regardless of leading zeros (a leading zero in an IPv4 address octet indicates octal notation). Note: address information may be enclosed inside "[]" but this form is not required. EXAMPLE SMTPD ACCESS MAP
/etc/postfix/main.cf: smtpd_client_restrictions = ... cidr:/etc/postfix/client.cidr ... /etc/postfix/client.cidr: # Rule order matters. Put more specific whitelist entries # before more general blacklist entries. 192.168.1.1 OK 192.168.0.0/16 REJECT 2001:db8::1 OK 2001:db8::/32 REJECT SEE ALSO
postmap(1), Postfix lookup table manager regexp_table(5), format of regular expression tables pcre_table(5), format of PCRE tables README FILES
Use "postconf readme_directory" or "postconf html_directory" to locate this information. DATABASE_README, Postfix lookup table overview HISTORY
CIDR table support was introduced with Postfix version 2.1. AUTHOR(S) The CIDR table lookup code was originally written by: Jozsef Kadlecsik KFKI Research Institute for Particle and Nuclear Physics POB. 49 1525 Budapest, Hungary Adopted and adapted by: Wietse Venema IBM T.J. Watson Research P.O. Box 704 Yorktown Heights, NY 10598, USA Wietse Venema Google, Inc. 111 8th Avenue New York, NY 10011, USA CIDR_TABLE(5)
All times are GMT -4. The time now is 04:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy