The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find a particular word in perl Harikrishna Shell Programming and Scripting 2 04-10-2008 03:35 AM
find a word in a file, and change a word beneath it ?? vikas027 Shell Programming and Scripting 2 02-13-2008 04:23 PM
Removing a specific word from a created list erest8 UNIX for Dummies Questions & Answers 5 10-14-2007 05:11 AM
Change Position of word character cedrichiu Shell Programming and Scripting 6 03-12-2007 01:52 AM
List all file which have specified word aungomarin UNIX for Advanced & Expert Users 4 01-03-2007 03:37 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-30-2008
pmpx pmpx is offline
Registered User
  
 

Join Date: Sep 2005
Location: Portugal
Posts: 20
Change word in list - PERL

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

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 (permalink)  
Old 06-30-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
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.....
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0