Change word in list - PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change word in list - PERL
# 1  
Old 06-30-2008
Change word in list - PERL

Hi,
probably a newbie question..... But there it goes. Smilie

I have a list words. This list is taken out of a file with grep and cut.
my @system = (`grep up system_list| cut -d':' -f1`
);

system=
sm00xp03
sm01ftp02
sm00ssh12

What I want to have is a list, with each of the words in the first list changed and resulting in two words, like this

new_list=
sr00xp01
sr00xp02
sr01ftp01
sr01ftp02
sr00ssh01
sr00ssh02

The chars that aren't in bold don't change from one list to the other.

Any ideia?

Thanks in adavance for any help given.
# 2  
Old 06-30-2008
Hello...

So let me understand this...

1. the second character always becomes an 'r'.
2. then there are 2 digits, and some number of characters (your example shows one 2 and two 3 character groups) that you don't change in the second list.
3. followed by 2 digits that do change in the second list, and always to a '01' and '02'.

Have I got that right? If I have then you might be able to do this with a pattern/substitution (or maybe 2 of them) inside a foreach loop...

foreach $word (@list) {
chomp($word); # just in case has newline
$word =~ s/s/r/;
$word =~ s/(\d\d\w+)/$1 . 01\n$1 . 02\n/;
$bigword = $bigword . $word;
}
(@new_list) = $bigword; # you concatenated everything into $bigword with newlines, so they should parse into the array by themselves ...

I have no idea of this would work, but it might... Otherwise there are other approaches, mainly to extract the second pattern above and just build the list element by element yourself....

$newctr = 0;
foreach $word (@list){
$word =~ /^(\w)/;
$first_letter = $1;
$word =~ /(\d\d\w+)/
$group = $1;
$new_list[$newctr] = $first_letter . 'r' . $group . "01";
$new_list[$newctr + 1] = $first_letter . 'r' . $group . "02";
$newctr += 2;
}

Now that I know will work, but it isn't very elegant.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Word change in a document

I have a bunch of documents where I need to change the word pi to pisignage. No big deal there: sed -i -e 's/pi/spisignage/g' /path/to/file However it is finding things like the word stopping and making the word stoppisignageng. Any suggestions to just find the word pi and change it? ... (3 Replies)
Discussion started by: wspgpete
3 Replies

2. Shell Programming and Scripting

Word change in a document

I have a bunch of documents where I need to change the word pi to pisignage. No big deal there: sed -i -e 's/pi/spisignage/g' /path/to/file However it is finding things like the word stopping and making the word stoppisignageng. Any suggestions to just find the work pi and change it? ... (0 Replies)
Discussion started by: wspgpete
0 Replies

3. Shell Programming and Scripting

Remove string perl with first or last word is in a list

Hello, I try to delete all strings if their first or last word is one of this list of words : "the", "i", "in", "there", "this", "with", "on", "we", "that", "of" For example if i have this string in an input file "with me" this string will be removed, Example: input "the european... (2 Replies)
Discussion started by: cyrine
2 Replies

4. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

5. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

6. Shell Programming and Scripting

perl (word by word check if a hash key)

Hi, Now i work in a code that 1-get data stored in the database in the form of hash table with a key field which is the " Name" 2-in the same time i open a txt file and loop through it word by word 3- which i have a problem in is that : I need to loop word by word and check if it is a... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

7. Shell Programming and Scripting

change first word in the every new line

Hello, i'm new with the scripting on unix and i need such script or maby something to that way: I need to change the the first word in every new line to a given word. Using just /bin/sh ( not using sed, awk, perl and ect). I would be very grateful. (9 Replies)
Discussion started by: wrwe
9 Replies

8. Shell Programming and Scripting

How to find the word and change it

Hi, I am writing a script where, i want to parse through $JAVA_OPTS, if it finds the word -cleint it should return "no change needed" or else it should print "change needed". I tried a lot, but could not get the proper output. I am new to unix shell. If u guys can guide me it would be of gr8... (4 Replies)
Discussion started by: asirohi
4 Replies

9. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies

10. Shell Programming and Scripting

Change Position of word character

Hi, I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field. 5874957|901125| 95874960|650614| 95874966|870308| 901125 to be changed as 25-11-1990 for eg Can someone help please ?? :) ... (6 Replies)
Discussion started by: cedrichiu
6 Replies
Login or Register to Ask a Question