Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace whole line where only part of line is known 3

Status
Not open for further replies.

aschneck

IS-IT--Management
Oct 31, 2002
12
DE
Hi,
I want to find lines in files that starts with a known string (but not the hole line is known) and than change the whole line (not only the search string) with another line/string.
Example:
file has line:
FROM TEXT: jdoe@org.com
search string is 'FROM TEXT:' because it's always the same
Then the whole line FROM TEXT: jdoe@org.com should be changed to FROM TEXT: helpdesk@org.com
Any ideas how I can do this?
Axel
 
Hi Axel,

Try this:
awk -F: -f awk_script <your_text_file

with awk_script:
BEGIN {
OFS=&quot;:&quot;
}
/FROM TEXT/ {print $1,&quot;helpdesk@org.com&quot;}
 
I'm guessing that you have a mixed text file/email type situation right?

If this is the case it's could be a pain to deal with. That's why we usually ask for a data sample so we can see the damage. ;)

I used this as a model:
################
FROM TEXT: bogus@nuevo893004749.newyear.org dyudms893kk23i9dmdlw
7ei3mndejcksdcmfvjdkldk: :FROM TEXT: newline@yahoo.com fmdi3ifrk3odf9fkidfkei9fjkf
jdflos898dm FROM TEXT: shutter@poo.jn.au.com kcppd0wl2md9c0-el;303kkfo
FROMmdols0s0TEXT,kdokldido9wksdnmcdkskl FROM TEXT: green@bogus.org 0,oeps
##################

Hopefully your data is more structured than this.

Using this:
awk ' {
if ($0 ~ /FROM TEXT/) {
match($0,/FROM TEXT:.*\..*[\t ]/)
print substr($0,RSTART,RLENGTH)
}
}' scrap.txt
FROM TEXT: bogus@nuevo893004749.newyear.org
FROM TEXT: newline@yahoo.com
FROM TEXT: shutter@poo.jn.au.com
FROM TEXT: green@bogus.org

You can get the all important start
and end of the needed string (if the regexp works, it needs whitespace at the end of the email address)

From there it's fairly simple to
assign the substring to a variable
and substitute away.

awk ' {
add = &quot;FROM TEXT: helpdesk@org.com &quot;
if ($0 ~ /FROM TEXT/) {
match($0,/FROM TEXT:.*\..*[\t ]/)
line = substr($0,RSTART,RLENGTH)
gsub(line,add,$0)
}
}' scrap.txt

FROM TEXT: helpdesk@org.com dyudms893kk23i9dmdlw
7ei3mndejcksdcmfvjdkldk: :FROM TEXT: helpdesk@org.com fmdi3ifrk3odf9fkidfkei9fjkf
jdflos898dm FROM TEXT: helpdesk@org.com kcppd0wl2md9c0-el;303kkfo
FROMmdols0s0TEXT,kdokldido9wksdnmcdkskl FROM TEXT: helpdesk@org.com 0,oeps

HTH



 
First to both of you thanks for the very fast answer! I'll try both of them.
Next sorry for my incomplete question. So, here a bit more information: No that is not a mixed file. What I wanna handle with is an text only file, but yes indeed it has to do with mail. I have a Mail Gateway that get mails from a third party app in form of a control file (thath is what I wanna change) and an according mail file. So, the file I wanna change is a text only on, that contains always the same text lines and I only wanna change two from-lines (FROM: and FROM TEXT:) with a predefined line.
Also I just found a mistake ther is no : in the search string...sigh
Here is an example one:
------snip----
WPC-API= 1.2;

Header-Char= T50;

Msg-Type= MAIL;

From-Text= USER@company.com;

From= USER@company.com;

To= rec@any.com;

To-Text= rec@any.com;

All-To= rec@any.com;

Security= Normal;

Send-Options= Notify-User;

Status-Request= Delivered, Opened;

Msg-Priority= Normal;

Msg-Char= T50;

ATTACH-FILE=

Current-File=ANHG5030.TXT;

;

Subject= 1559652 ES 02 14136;

-END-
-------------------------snap
Axel
 
You can just modify the solution I gave to handle this
much more simply:
awk ' {
line[1]=&quot;FROM: Me@here.com; &quot;
line[2]=&quot;FROM TEXT: Me@here.com; &quot;
gsub(/FROM=.*;/,line[1],$0)
gsub(/FROM-TEXT=.*;/,line[2],$0)
}' file

 
Thanks again. So I'll try that, but before I start rtying it, will this also change the file? Sorry, maybe a stupid question but im a bit new in AWK.
Axel
 
No.
All changes are performed without modifying the original
file.

To commit the changes something like this could
be done:
tmp=&quot;/home/tmp.txt&quot;
awk ' {
line[1]=&quot;FROM: Me@here.com; &quot;
line[2]=&quot;FROM TEXT: Me@here.com; &quot;
gsub(/FROM=.*;/,line[1],$0)
gsub(/FROM-TEXT=.*;/,line[2],$0)
print
}' filename > $tmp && mv $tmp filename
 
Hi MARSD, thanks again, it's working.
But after finishing my 600lines script I found that there is another question...hmmm ;-)
As you look in my example ther is a line like
Current-File=ANHG5030.TXT
This line points which file will be used as mailtext with the header file. Means the name of the according file is here ANHG5030.TXT.
No if I wanna extract this name so I can check whether it exists or not I wonder how I can do this. Normaly it should work with an awk print$2 when delimiter is set to =, I think.
But first i must finde the line that starts with Current-File.
Can you help again please?
Kind regards and a happy new year,
Axel
 
Okay using the original script:
awk ' {
line[1]=&quot;FROM: Me@here.com; &quot;
line[2]=&quot;FROM TEXT: Me@here.com; &quot;
gsub(/FROM=.*;/,line[1],$0)
gsub(/FROM-TEXT=.*;/,line[2],$0)
if ($0 ~ /Current-File/) {
p = fileExists($0) == 0 ? &quot;File exists&quot; : &quot;No file&quot;
print p, $0
}
}' file

The fileExists function could look like this:
function fileExists(line) {
split(line,arr,&quot;=&quot;)
sub(/;/,&quot;&quot;,arr[2])
cmd = &quot;test -e &quot; arr[2] &quot;; echo $?&quot;
cmd | getline var ; return var
}

A heuristic depending on the shell command return value.
Hopefully vgersh or one of the other guys has a better idea. I have no method right now for win32.
 
how about:

function fileExists(file, ret)
{
ret=0;
#printf(&quot;DEBUG: fileExists - [%s]\n&quot;, file) | &quot;cat 1>&2&quot;;
if ((getline dummy < file) > 0) {
# file exists and can be read
ret = 1;
close(file);
}
return ret;
} vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top