Text replacement between 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Text replacement between 2 files
# 1  
Old 09-06-2007
Power Text replacement between 2 files

I have 2 files that are tab dilimiter:

file1 contains:
T 1 2 3 1000
T 5 10 15 9000
T 4 5 6 2000
T 3 7 9 6000
AND SO ON

file2 contains: (columns number 1, 2, and 3 are match-pattern to file1)
1 2 3 JOHN
4 4 4 MIKE
4 5 6 TOM
3 7 9 MIKE
AND SO ON


I want file3 contains the following result:

T 1 2 3 JOHN 1000
T 4 5 6 TOM 9000
T 3 7 9 MIKE 6000
AND SO ON


Please advise! I tried using sed with no success!
# 2  
Old 09-06-2007
Code:
$ ./process_it.sh 
T       1       2       3       JOHN    1000
T       4       5       6       TOM     2000
T       3       7       9       MIKE    6000
$ cat ./process_it.sh 
#!/bin/bash

FILE1=file1
FILE2=file2

# All FS are a literal tab - use \t if your awk supports it
while read LINE; do
   ID=$( echo "${LINE}" | awk 'BEGIN {FS="      "} {printf("%s\t%s\t%s\n",$2,$3,$4)}' )
   MATCH=$( egrep "^${ID}" ${FILE2} )
   if [ "${MATCH}" != "" ]; then
      PART1=$( echo "${LINE}" | awk 'BEGIN {FS="        "} {print $1}' )
      PART2=$( echo "${LINE}" | awk 'BEGIN {FS="        "} {print $NF}' )
      printf "${PART1}\t${MATCH}\t${PART2}\n"
   fi
done < ${FILE1}

exit 0

Cheers,
ZB
# 3  
Old 09-06-2007
Quote:
Originally Posted by bobo
I tried using sed with no success!
next time post your code, and tell us what error messages you have got if any. Don't just say "does not work".
Code:
awk 'FNR==NR{arr[$2" "$3" "$4]=$0;next}
{
        s=$1" "$2" "$3
        if(s in arr){
                print arr[s],$NF
        }
}
' "file" "file1"

output:
Code:
 ./testnew.sh
T 1 2 3 1000 JOHN
T 4 5 6 2000 TOM
T 3 7 9 6000 MIKE

# 4  
Old 09-10-2007
Power

ZB and Ghostdog74,

I want to thank you for your reply! These are great codes! I'm still working my program and tried to modify a few thing to it. I might come back for more questions.

These are the code I began with:

#!/bin/ksh
IFS=" "
while read a b c d; do
line3=$printf nawk -F ' ' '{print $1, $2, $3, $4}' | sed 's/ / /'g
line2=$printf nawk -F ' ' '{print $1, $2, $3}' | sed 's/ / /'g
sed 's/$line2/$line3/'g file1 >> file3
done<file2


I got no error messages. It just don't work like I think it should!

Many thanks friends!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed text replacement

Hello, I'm using Bash and Sed to replace text within a text file (1.txt) twice in one script. Using a for loop I'm initially replacing any 'apple' words with the variable 'word1' ("leg). I'm then using another for loop to replace any 'apple' words with the variable 'word2' ("arm"). This task is... (2 Replies)
Discussion started by: Flip-Flop
2 Replies

2. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

4. Shell Programming and Scripting

Conditional replacement of columns in a text file

Hello scriping expert friends, I have 2 requirements on replacing fields of text files: I have lot of data with contents like below: Requirement-1: The digit after 0 should always be changed to 1 (3 Replies)
Discussion started by: magnus29
3 Replies

5. Shell Programming and Scripting

Block of text replacement using sed

Hi, I have a requirement in which i need to replace text as below - <stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt; &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt; &lt;Param Name=&quot;Value&quot;... (0 Replies)
Discussion started by: abhitanshu
0 Replies

6. UNIX for Dummies Questions & Answers

Simple awk script for positional replacement in text?

I have a string of letters. (They happen to be DNA, not that it's relevant to the question.) For analysis purposes, I need to replace the information at some of the sites. I need to do this based on their position, not the information in that position. I also need to ignore differences at other... (10 Replies)
Discussion started by: JFS
10 Replies

7. Shell Programming and Scripting

Replacing text based on replacement tables

Dear all, will be grateful for your advices.. The need is (i guess) simple for UNIX experts. Basically, there are replacement tables, which would be used to replace text strings in the data (large volumes..). An exmpl table (a "config file"): VIFIS1_1_PE1836 VIBRIO_FISCHERI VIPAR1_1_PE1662 ... (7 Replies)
Discussion started by: roussine
7 Replies

8. Shell Programming and Scripting

Replacement of text in a file

Hi , I have some data in my file(properties.txt) like this. # agent.properties agent.dmp.Location= agent.name= I need to relpace the agent.dmp.location with agent.dmp.Location = /opt/VRTS/vxvm I am using the follwing to replace the string AGENT_NAME=snmp... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies

9. UNIX for Dummies Questions & Answers

Sed text replacement issue.

Hi, Im trying to find and replace text within a unix file using sed. The command that i have been using is sed '/,null,/ s//, ,/g' result.txt>result.tmp for replacing ",null," with ", ,". But this only replaces the first occurrance of ,null, in every line. I want to do it globally. It... (7 Replies)
Discussion started by: sohaibs
7 Replies

10. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies
Login or Register to Ask a Question