Mapped list replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mapped list replacement
# 1  
Old 10-08-2008
Mapped list replacement

Hi to all. I've looked for this, and haven't found it, but please pardon me if I've overlooked something. I'm trying to come up with a relatively simple command line search and replace from and to which I can easily pipe. In particular, let's suppose there are 2 lists, A and B. If a string appears on list A, replace it with its corresponding member on list B. For example, let's replace heroes with villains:

ListA,ListB
Superman,Lex_Luthor
Batman,The_Joker
ProfessorX,Magneto

So, anywhere 'Superman' appears, replace him with 'Lex_Luthor' and so on. So, in a perfect world, 'tr' might be expanded to do this. This doesn't work, of course, so please treat this as pseudocode:

Code:
tr 'Superman,Batman,ProfessorX' 'Lex_Luthor,The_Joker,Magneto'

Something that actually does work is:

Code:
 sed -e 's/Superman/Lex_Luthor/' -e 's/Batman/The_Joker/' -e 's/ProfessorX/Magneto/'

That works, and is an acceptable solution, but will grow pretty quickly with additional terms. Is there a more compact way of accomplishing this? Many thanks in advance.
# 2  
Old 10-08-2008
Iam not sure if these keywords are appearing in a file. Have you tried considering SET command
You need to replace "," by space using "tr"
Read file and each line use can use set command then replace $2 with $1 within a loop ?

the way set works is as
set hi there it is a test
if you echo $3 it prints out "it"
similary as long as there is a space you can print Variables as $1 $2 $3 $4 using set.
Hope this sorts out your problem. Good luck
# 3  
Old 10-08-2008
Quote:
Originally Posted by ashishparimoo
Iam not sure if these keywords are appearing in a file. Have you tried considering SET command
You need to replace "," by space using "tr"
Read file and each line use can use set command then replace $2 with $1 within a loop ?

the way set works is as
set hi there it is a test
if you echo $3 it prints out "it"
similary as long as there is a space you can print Variables as $1 $2 $3 $4 using set.
Hope this sorts out your problem. Good luck
No, they don't appear in an actual list. I was just using that as a way of picturing what replaces what. In fact, the question that prompted this was that a particular log file uses MON format for the month (that is, spelled-out, first 3 letters, such as Jan, Feb, etc). We needed this to be converted to numeric format, such that Jan becomes 01, Feb becomes 02, and so on. I ended up doing:

Code:
<input> | sed -e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' -e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' -e 's/Jul/07/' -e 's/Aug/08/' -e 's
/Sep/09/' -e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/'`

That worked, but if there's a more convenient and compact way to do this, I'd like to know it.
# 4  
Old 10-08-2008
I had to write the same script in which i had input as number it needed to convert to alphabets. Here is a sample that function .

function r_Keys() {
KeyMap=("0" "Zero" "1" "One" "2" "Two" "3" "Three" "4" "Four" "5" "Five" "6" "Six" "7" "Seven" "8" "Eight" "9" "Nine")
myKey[0]=`echo $1| cut -c 1`
myKey[1]=`echo $1| cut -c 2`
myKey[2]=`echo $1| cut -c 3`
myKey[3]=`echo $1| cut -c 4`
j=0;
for key in ${myKey[@]}
do
for (( i = 0 ; i < ${#KeyMap[@]} ; i=i+2 ))
do
if [ "$key" == "${KeyMap[$i]}" ]
then
roomKey[$j]="${KeyMap[$i+1]}"
fi
done
j=`expr $j + 1`
done
return

Just pass the function any numberal it will return you the equivalent alphabet. Its little complicated but definetly not the compact Smilie
Good luck

Last edited by ashishparimoo; 10-08-2008 at 05:55 PM.. Reason: had to be otherway round
# 5  
Old 10-08-2008
Quote:
Originally Posted by treesloth
No, they don't appear in an actual list. I was just using that as a way of picturing what replaces what. In fact, the question that prompted this was that a particular log file uses MON format for the month (that is, spelled-out, first 3 letters, such as Jan, Feb, etc). We needed this to be converted to numeric format, such that Jan becomes 01, Feb becomes 02, and so on. I ended up doing:

Code:
<input> | sed -e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' -e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' -e 's/Jul/07/' -e 's/Aug/08/' -e 's
/Sep/09/' -e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/'`

That worked, but if there's a more convenient and compact way to do this, I'd like to know it.
Here's something you can try if you have Perl and the Date::Manip module installed:

Code:
echo "Jan 24, 2007"|perl -pe 'use Date::Manip;$m=UnixDate(ParseDate($_),"%b");$n=UnixDate(ParseDate($_),"%m");$_=~s/$m/$n/g;'
01 24, 2007

echo "4 Mar 2008"|perl -pe 'use Date::Manip;$m=UnixDate(ParseDate($_),"%b");$n=UnixDate(ParseDate($_),"%m");$_=~s/$m/$n/g;'
4 03 2008

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

mapped reads using samtools flagstat options

Hey guys, Does anyone know how to calculate total number of mapped reads for a certain region of a bam file using samtools flagstat? I know to use flagstat for the whole bam file. but for a specific region of it... is there an option? Tnx ---------- Post updated at 12:27 PM ----------... (1 Reply)
Discussion started by: @man
1 Replies

2. Red Hat

Crontab not generating file in mapped drive in windows

Dear Experts, I've schedule scripts with the help of Cronjob, and output should be generated on windows mapped drive, Earlier 2 days its was working fine and now my cronjob is not generating file on windows network drive. Please suggest. Linux details given below. $ tail... (9 Replies)
Discussion started by: Mohammed Fareed
9 Replies

3. AIX

how will i know if a lun has been already mapped to a vio client

Hi im logged in to the vio servers now. when i give # lspv | wc -l i get the count as 6246 how will i know if a lun has been already mapped to a vio client or it is left free without mapping to any of the vio client ? (1 Reply)
Discussion started by: newtoaixos
1 Replies

4. Shell Programming and Scripting

mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting: here is my question: i m having two files. 1st file say a.txt contain 3 column like SPECIALITY|UMP_CODE|SPECIALTY_CODE Addictive Diseases|25ADD|ADD Addictive Diseases/Family Practice|25ADD|ADD/FP Aerospace Medicine|1.041666667|AM... (4 Replies)
Discussion started by: dsh007
4 Replies

5. Solaris

Unexpected mapped volume in Solaris 10

Hi everybody, I'm facing a little trouble with my system. I have a SPARC M4000 server, which connected to a SAN with 2 Brocade 300 Switches and 1 STK 6140. The M4000 has 2 FC HBA cards, one port per card. Each card connect to one switch as follows... ____________M4000____________ ___FC-HBA... (2 Replies)
Discussion started by: nam.nguyen
2 Replies

6. Programming

segmantation Fault error SEGV_MAPERR - Address not mapped to object

Program received signal SIGSEGV, Segmentation fault si_code: 1 - SEGV_MAPERR - Address not mapped to object. 0x9fffffffbe7080d0:0 in free+0xb0 () from /usr/lib/hpux64/libc.so.1 Hi , I have developed a class to read config file (flat file with space as a field seperator ) on plattform... (3 Replies)
Discussion started by: pravinbhingare
3 Replies

7. UNIX for Advanced & Expert Users

Linux: Access time of mapped data

Before I forget, I'm running on a RedHat 5 box with the following uname -a output: Linux gnc141c 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux Now on to my question. I'm using a tool that maps a Matlab .mat file using the Linux mmap functionality and then... (1 Reply)
Discussion started by: rusttree
1 Replies

8. SCO

SCO Mapped Drive

We are running SCO OpenServer 5.0.7. Some of my users have drives mapped to the SCO box, some don't. The consultant, who setup the mapped drives, is long gone, and I need more users to have drive letter mapping. When I try to map a drive via windows, I am repeatedly asked for a user/password,... (1 Reply)
Discussion started by: bnhcomputing
1 Replies

9. Filesystems, Disks and Memory

Virtual Machine and Mapped Raw LUN's

Hello, I have a server running CentOS 5.1 on a virtual machine. Right now, there is one virtual disk on the system. Below are the result when I run the df commmand: Filesystem.....1K-blocks.........Used.....Available....Use%.....Mounted on... (1 Reply)
Discussion started by: kennyw1000
1 Replies

10. Linux

How to access mapped dirve in Linux

i want to access mounted/mapped drive in linux using C code is it possible to access the mapped drive in linux using C code ? waiting for replay NIrav (0 Replies)
Discussion started by: niravuchat
0 Replies
Login or Register to Ask a Question