Text Translation


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Text Translation
# 1  
Old 03-01-2011
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>
Code:
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>
Code:
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"
Code:
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

Last edited by Scott; 03-01-2011 at 02:19 PM.. Reason: Please use code tags.
# 2  
Old 03-01-2011
Note that lines conaining none of the given pattern will not be displayed :

Code:
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 :

Code:
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
Code:
/Original/{y="TEXT1"}

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

for a case non-sensitive comparison

Last edited by ctsgnb; 03-01-2011 at 02:01 PM..
# 3  
Old 03-01-2011
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


Code:
$ 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.
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 03-01-2011
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.

Code:
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

Last edited by Scott; 03-01-2011 at 06:12 PM.. Reason: Code tags
# 5  
Old 03-02-2011
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:

Code:
2837 TEXT5 3747

if the input is
Code:
 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. IP Networking

Network address translation

How would one approach the problem of determining the NAT tables of a router without knowing the userid and password. The only password holder died. I know the internal ip address of the router is 192.168.2.1, and also ports 80 or 8080 and 3389 are open. (5 Replies)
Discussion started by: jgt
5 Replies

2. AIX

Can't ping - IP translation issue?

Hello, Any help or advice on this would be greatly appreciated. I have a site-to-site VPN, across which data is synced between two CRM systems. The opposing side of this tunnel is a flat network (lets call it 201.200.156.xx/24) My internal network is a 192.168.0.xx/24 network, however before... (2 Replies)
Discussion started by: thruster9
2 Replies

3. UNIX for Dummies Questions & Answers

AIX to HP-UX translation please

I have been looking up the allocated space, free space and the contents (raw, jfs, paging) of the logical volumes in AIX servers. I have been using lsvg and lsvg -l (server name). What is the HP-UX equivalent to that? I was given this command also so I would not have to type in each volume group... (2 Replies)
Discussion started by: compan023
2 Replies

4. UNIX for Dummies Questions & Answers

help --translation

I am reading the SANS security policy--.I want to translate the to Chinese.But I don`t understand a paragraph of the. It is :"Where SNMP is used, the community strings must be defined as something other than the standard defaults of "public," "private" and "system" and must be different from... (1 Reply)
Discussion started by: beijingzhzj
1 Replies

5. Shell Programming and Scripting

I need translation!

Someone give me info on this script. I put all the parts I do not understand in BOLD. Please someone explain what the parts in BOLD does/mean. Thanks! #!/bin/bash #Usage: cmdArgs.sh arg1 arg2 arg3 printf "The number of command line args is: $#\n" declare -i counter #Not strictly... (7 Replies)
Discussion started by: 801238429
7 Replies

6. Programming

Swedish Character translation

Sorry if this been posted before. At the moment I'm having problem with the iscntrl() function. I need it to run a check on code entered into a Text Box, however I also need to allow the "Ö, ö" Swedish characters. The iscntrl() function seems to pick up the characters above as part of the... (0 Replies)
Discussion started by: rebelwassie
0 Replies

7. Shell Programming and Scripting

need translation?

Can someone tell me exactly what this code is doing? # # gnutest # # Test launch of ghostscript (gs) from a script # rm gnutest.ps # # ------- calculation that would generate file(s) to # be plotted with gnuplot would be placed here ----------- # gnuplot << \E-o-f2 set... (1 Reply)
Discussion started by: wmosley2
1 Replies
Login or Register to Ask a Question