Text Translation

hi all,
Im a unix newbie and Im trying to find and replace text and I been looking at these posts since 7 am and I still dont see what Im
after.
Here is the task at hand. Im trying to translate few lines by doing cat <file> and here is what Im doing
<file>

20110228 Mike Original apartment costed almost 23277AA
20110130 And few of the upgrades alone were into few hundred thousand dollars 47738
20110219 BUT THAT WAs magical and everyone was so elated to see 93884
20110222 so would you want to join us 38488

<expected file>

20110228 TEXT1 23277AA
20110130 TEXT2 47738
20110219 TEXT3 93884
20110222 TEXT4 38488

in other words
if the line contains the word, original, then replace that entire text(only alphabets) line with "TEXT1"
if the line contains the word, dollars, then replace entire text with "TEXT2"
if the line contains the word, magical, then replace entire text with "TEXT3"
if the line contains the word, join, then replace entire text with "TEXT4"

cat <file> | awk '$0 ~/original/ { sub($0, "TEXT1"; print }'

but that is not working as I dont know what Im doing here.

Please help !

Thanks,
PGonzalez

Note that lines conaining none of the given pattern will not be displayed :

nawk '/Original/{y="TEXT1"}/dollars/{y="TEXT2"}/magical/{y="TEXT3"}/join/{y="TEXT4"}y{x=$NF;print $1,y,x;y=t}' infile

If you want them be displayed :

nawk '/Original/{y="TEXT1"}/dollars/{y="TEXT2"}/magical/{y="TEXT3"}/join/{y="TEXT4"}{x=$NF;print (y)?$1 FS y FS x:$0;y=t}' infile

You could also replace

/Original/{y="TEXT1"}

with

(tolower($0))~/original/{y="TEXT1"}

for a case non-sensitive comparison

That is a useless use of cat. Whenever you do cat foo | program you can also do program < foo to save your program a lot of wasted CPU time.

For programs that take filenames, like awk does, you can even do
program foo

$ awk '{
         if($0 ~/original/ )
                 print "TEXT1"
         else if($0 ~/dollars/)
                 print "TEXT2"
         else
                 print $0
 }
 ' datafile
20110228 Mike Original apartment costed almost 23277AA
TEXT2
20110219 BUT THAT WAs magical and everyone was so elated to see 93884
20110222 so would you want to join us 38488

Note that original and Original are not the same.

2 Likes

Corona688 & Ctsgnb,

Both of your stuff worked very very well.

Thanks a bunch.

PGonzalez

---------- Post updated at 02:39 PM ---------- Previous update was at 02:16 PM ----------

Ctsgnb,

Your solution worked great without replacing the other text. Now how to make this work for another pattern lets say if the text contains a line as below -

2837 Opera Singers wanted to adjust the reecho clarity on 3747

In the above line, I have adjust and want to incorporate that into this line here below and I cant seem to do it.

Please help.

nawk '/Original/{y="TEXT1"}/dollars/{y="TEXT2"}/magical/{y="TEXT3"}/join/{y="TEXT4"}{x=$NF;print (y)?$1 FS y FS x:$0;y=t}' infile

Also, strangely enough, Im seeing some of the old text along with the newly replaced text as well. I dont know how to go about this now. I only tested part of the file in the morning and now, when I run this on the entire, Im seeing all of these new errors.

---------- Post updated at 04:46 PM ---------- Previous update was at 02:39 PM ----------

Ctsgnb,

Can you please tell me as how do I add another conditional check in here so as to increase the scope of what you wrote in here ?

thanks,
PGonzalez

what do you want to do with that line ?

Since it matches none of the 4 conditions defined, what do you want :

1) since it match none of the 4 previous conditions, you want just to skip it ?
2) since it match none of the 4 previous conditions, you want just to display it ?
3) You want to add the 5th condition (which one ? ) ... to replace it with for example:

2837 TEXT5 3747

if the input is

 2837 Opera Singers wanted to adjust the reecho clarity on 3747

please let me know which output you need.

Please give more clue about what input you have and what output you expect.