Need help with scripting (find and replace)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with scripting (find and replace)
# 1  
Old 05-02-2011
Need help with scripting (find and replace)

Hello.

I've got to files (source and target).

1) target.txt:
Code:
....
#: tracopt/mimeview/php.py:97                                                                                                                                
msgid ""                                                                                                                                                     
"You appear to be using the PHP CGI binary. Trac requires the CLI version "                                                                                  
"for syntax highlighting."                                                                                                                                   
msgstr ""                                                                                                                                                    
"Похоже, вы используете CGI-версию PHP. Trac для подсветки синтаксиса "                                                                                      
"требуется CLI-версия."                                                                                                                                      
                                                                                                                                                             
#: tracopt/ticket/deleter.py:69 tracopt/ticket/deleter.py:82                                                                                                 
#: trac/ticket/templates/report_list.html:35                                                                                                                 
msgid "Delete"                                                                                                                                               
msgstr "Удалить"                                                                                                                                             
                                                                                                                                                             
#: tracopt/ticket/deleter.py:70 tracopt/ticket/templates/ticket_delete.html:43                                                                               
msgid "Delete ticket"                                                                                                                                        
msgstr "Удалить карточку"  
....

2) source.txt
Code:
...
msgstr ""                                                                                                                                                    
msgstr ""                                                                                                                                                    
msgstr "Dzēst"                                                                                                                                               
msgstr "Dzēst Card"                                                                                                                                          
msgstr "Dzēst komentāru% (num) s"                                                                                                                            
msgstr "Card #% (id) as anonīmus."                                                                                                                           
msgstr "Komentārs% (num) s par kartes #% (id) as anonīmus."                                                                                                  
msgstr "Komentārs% (num) s nav atrasts"                                                                                                       
...

Task:
Need to find all strings in target file that have msgstr in the begining and replace it with strings from source file
Count of lines with msgstr is the same for both of files. Overall count of strings - not the same, cause of the comments and msgid-strings.

Thanks in advance.
P.S. Sorry for my poor English
# 2  
Old 05-02-2011
source.txt has 8 lines of data. Assuming that you want -
one replacement per line of text in source.txt, i.e., 8 replacements.
There also has to be exactly the same number of lines with "msgstr" in source.txt as there are "msgstr" lines
in the target.txt file.
Code:
awk 'FILENAME=="source.txt" { arr[FNF]= $2 }
      FILENAME=="target.txt" { if( /^msgstr/ ~$1) {$2=arr[i++]}; print $0} ' source.txt target.txt > newfile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace character in shell scripting

Hi, I need to replace the space " " with underscore "_" using shell scripting. The data are inside the text file. Is there are any simple code to that.? (3 Replies)
Discussion started by: gopishrine
3 Replies

2. Shell Programming and Scripting

Search and replace in shell scripting

I am trying to write shell script to find and replace using Sed. but i am unable to complete the setting. need help in doing that. Requirement: FROM "${O_INSTANCE}/diag/logs/${C_TYPE}/${C_NAME}/httpd.pid" TO "/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid" (10 Replies)
Discussion started by: avmk0407
10 Replies

3. UNIX for Beginners Questions & Answers

Shell Scripting , need to search and replace a string in AIX

Hi Guys, I need to search and replace a string in AIX using variables and should be case insensitive. I am able to search and replace using below command but its not working as case insensitive. cat abc.txt | sed -e 's/$a/$b/g' > abc.txt But i need to perform this with case... (9 Replies)
Discussion started by: mohit_vardhani
9 Replies

4. Shell Programming and Scripting

Find and Replace in UNIX Scripting

Hi, Need your advices, Input : select code,status,input from VIEW1.VIEWNAME where IDU_CD IN ('S','N') and status_col='derived')) union select code,status,input from VIEW1.VIEWNAME2 where date='#p1' Expected output : select code,status,input from VIEW1.VIEWNAME where... (2 Replies)
Discussion started by: Nandy
2 Replies

5. Shell Programming and Scripting

Scripting a global find and replace in an VME output print file

Hi Folks, Below is an extract from a VME Print file which gets handed over to a print house. The problem I have is not that tricky rther looking for a way to handle it in a simple and clean way. Is to first select all lines with "0058" which have four spaces so "0058 " as the selcetion... (3 Replies)
Discussion started by: Gary Hay
3 Replies

6. Shell Programming and Scripting

How to replace word with multiline text using shell scripting.

Hi all I have data files which contain data as shown below: Line 5: FIDE INST_DESC: DIAM Co Ltd/Japan => MAID Co Ltd/Japan INST_NME: DIAM Co Ltd/Japan => MAID Co Ltd/Japan Line 6: FIDE INST_DESC: DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the... (6 Replies)
Discussion started by: Ganesh_more
6 Replies

7. Shell Programming and Scripting

how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory) -68395#svg.xml -56789#ghi.xml -67894#gjk.org -56734#gil.txt I... (6 Replies)
Discussion started by: pratima.kumari
6 Replies

8. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

9. Shell Programming and Scripting

Sed scripting, match text within line and replace

New to sed... Have a file foo.txt (below). Need to replace text on 2 lines, but can only feed sed the first few characters of each line (all lines are unique). So, in my example, I have put '$' in place of what I need to figure out how to feed the whole line. What I have thus far: sed -e... (6 Replies)
Discussion started by: boolean2222
6 Replies

10. UNIX for Dummies Questions & Answers

Find and replace in all files using shell scripting

Hi all, I'm looking to find and replace a string in all HTML files within a certain directory, including subdirectories. Normally, I would play with this a little to get it to work, but I can't mess this up, so I'm going to ask here. Basically, I want to find "<title>" in all *.htm* files... (11 Replies)
Discussion started by: slothario
11 Replies
Login or Register to Ask a Question