Take strings from one file to replace others in another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Take strings from one file to replace others in another file
# 1  
Old 10-20-2008
Question Take strings from one file to replace others in another file

Dear people, I have an inquiry..

My need is to identify all strings marked at the beginning with unique "character1" in one file and replace those by strings marked with unique "character2" from another file. The important is to replace them strictly in order or occurence in both files.. Also, the "characters" 1 and 2 are important identifiers and must stay in their original files (only the rest of strings is replaced).

Is that possible to do such with a combination of grep and sed?

thanks a lot for your valuable help

Last edited by roussine; 10-20-2008 at 02:55 PM..
# 2  
Old 10-20-2008
Show a sample of each file's contents and how the final result should look like.
# 3  
Old 10-20-2008
file1:

>word1
>word2
>word3

the identifier is '>', the 'word..' can be whatever.

file2:
Query= wordx
Query= wordy
Query= wordz

the identifier is 'Query= ', the 'word..' is not identical to the one in file1, but comes in the right order (word1~wordx).

the result:

>wordx
>wordy
>wordz
# 4  
Old 10-21-2008
Does the first file have a space after the ">"? If so, look at the "join" command.
# 5  
Old 10-21-2008
Quote:
Originally Posted by geekosaur
Does the first file have a space after the ">"? If so, look at the "join" command.
no, it doesn't. I will try to study the 'join' to see if it helps, thanks.
# 6  
Old 10-21-2008
Data

Quote:
Originally Posted by geekosaur
Does the first file have a space after the ">"? If so, look at the "join" command.
oh, I guess the problem with join is that the lines in each file are separated by other lines, whitout the identifiers and with diffeent content (didn't mention this before, sorry..). And their order must not be broken, so they cannot be sorted before using join.

So the files are:

file1:

>word1
ererererererererer
>word2
ghghghghghg
hjhjhjhjhjh
>word3
bnbnbnbnbnbnb

the identifier is '>', the 'word..' can be whatever, the lines are separated by lines with whatever content.

file2:

Query= wordA
ghghgffkjghrhrhrh
qpqpqpqpqpqpqpqq
Query= wordB
fhfhfhfhf gjgjgj hkhkhkh
tyt5757
Query= wordC
ghghdjdjdj00

the identifier is 'Query= ', the 'word..' is not identical to the one in file1, but comes in the right order (word1~wordA, word2~wordB, etc). There is a space after the 'Query=', the lines are separated by other lines with different content.

the needed result:

>wordA
ererererererererer
>wordB
ghghghghghg
hjhjhjhjhjh
>wordC
bnbnbnbnbnbnb

Maybe someone has a idea how to do this automatically into shell?
# 7  
Old 10-21-2008
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
% head file?
==> file1 <==
>word1
ererererererererer
>word2
ghghghghghg
hjhjhjhjhjh
>word3
bnbnbnbnbnbnb

==> file2 <==
Query= wordA
ghghgffkjghrhrhrh
qpqpqpqpqpqpqpqq
Query= wordB
fhfhfhfhf gjgjgj hkhkhkh
tyt5757
Query= wordC
ghghdjdjdj00


% awk 'NR == FNR {
  if(/^Query= /)
    _[++count1] = ">"$2
  next
  }
(/^>/ && $0 = _[++count2]) || 1
' file2 file1
>wordA
ererererererererer
>wordB
ghghghghghg
hjhjhjhjhjh
>wordC
bnbnbnbnbnbnb

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

2. Shell Programming and Scripting

Replace multiple strings of a file

Hi, I have an array variable "arr" that reads string from a file "vari.txt". Thus, the array will be of variable length depending how many entries are present in "vari.txt" I use a for loop to traverse through the array. vari.txt (in this sample we have 2 entries, but it can have more... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Replace the .txt file between two strings in XML file

Hi i am having XML file with many number of lines,I need to replace between two strings with .txt file using awk. For ex <PersonInfoShipTo ------------------------------ /> My requirement is to replace the content between <PersonInfoShipTo ------------------------------ /> help me. Thanks... (9 Replies)
Discussion started by: Padmanabhan
9 Replies

4. Shell Programming and Scripting

Replace Contents between 2 strings in a file with contens of another file

Please I want to replace all the contents beween "Section" and "Ensection" in file1 with all contents in file2. Example: file1: Section "Screen" DefaultDepth 24 SubSection "Display" Depth 8 ViewPort 0 0 Modes "1024x768" "800x600" "640x480" EndSubsection SubSection "Display" Depth... (9 Replies)
Discussion started by: powelltallen
9 Replies

5. Shell Programming and Scripting

Replace the strings in file

Hello members, I been following this forums since very long time. I need to do one job. In my script I am evaluating one variable, lets say n=100. Now i have xml file inside which i need to replace the numbers in the desired lines with the evaluated number(n) +1. For example let's say... (4 Replies)
Discussion started by: mailtosaiki
4 Replies

6. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

Replace multiple strings in a file.

Hello Freinds, Hope, you all are doing well. I have to replace the static strings from one file (File 1) with the dynamic strings from the another file (File2). I've written a shell script for this.Below is the contents. while read line do field=`echo $line | awk '{print $1}'` ... (9 Replies)
Discussion started by: singh.chandan18
9 Replies

9. Shell Programming and Scripting

Shell script to replace strings to and from a file

Hello All, I have 2 files 1 ) source file eg asasa 1.2.3.4 adfhsdfsdfasdf zxzxzx 2.3.4.56 dsadasdasdsadasd kjjkjkjk 30.3.4.5 asdsadsadsadsadsad vxcvxcvx 1.2.3.4 qwewqewqeqweqwe 2) patern file 1.2.3.4 A 2.3.4.56 B 30.3.4.5 C I need the source to be changed to asasa A... (2 Replies)
Discussion started by: nitinkgoud
2 Replies

10. Shell Programming and Scripting

replace strings in a file

Hi I want to change the following passwd: files nis group: files nis in /etc/nsswitch.conf to be passwd: files compat group: files compat I tried cp -p nsswitch.conf nsswitch.conf.old (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question