perl- read search and replace string from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl- read search and replace string from the file
# 1  
Old 07-12-2012
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,

Code:
# s #; 0.123
# p #; 12.3
# d #; -2.33
# f #; 5.68
<blank line>
sssssss
spfdffff
sdfffffff

Now I want "s" should be replaced by 0.123 in last three lines, "d" by -2.33 etc. Number of alphabetical characters vary in each file and also the number of lines below the blank line may vary.
I am seeking for a perl program preferably, to automate text processing.
-regards and thanks,
satti
# 2  
Old 07-12-2012
Please post desired output for this sample data.
# 3  
Old 07-12-2012
dear bartus11 and others,
The desired output would be as :
Code:
0.123 0.123 0.123 0.123 0.123 0.123 0.123 
0.123 12.3 5.68 -2.33 5.68 5.68 5.68 5.68 
0.123 -2.33 5.68 5.68 5.68 5.68 5.68 5.68 5.68

thnx,
satti

Last edited by Franklin52; 08-06-2012 at 03:49 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 07-12-2012
Try:
Code:
awk '/^#/{a[$2]=$4;next}{n=split($0,b,"");for (i=1;i<=n;i++) printf a[b[i]]" ";printf "\n"}' file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 08-05-2012
Thanks a lot for a working code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. 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

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

5. Shell Programming and Scripting

How to read file, and replace certain string with another string?

Hi all, the value in the following file is just an example. It could be a different value/network addresses. Here is my example of initial output in a file name net.txt Initial Output, net.txt The goal is to produce the following format which is to convert from CIDR to Netmask... (6 Replies)
Discussion started by: type8code0
6 Replies

6. Programming

C program to read a binary file and search for a string?

Hi, I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language book:o At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Search for a string in a file and replace

I have file t1.log Contents of t1.log file Number of records processed: Number of records rejected: Error : xyz .......... abc .......... aaa _] start time : end time : Please let me know how i can remove the contents highlighted in red in the t1.log file. Thanks Sam (3 Replies)
Discussion started by: sam777
3 Replies

8. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

9. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies

10. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question