LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2010, 11:24 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
perl chop numbers off os a string - leave letters.


I am trying to strip the numbers from the 5th columns and just get letters - in perl - i tried using subsrt, chop. and a regex - anybody got any ideas?
casper@casper-laptop:~$ more myfile
procmonES snoc active 21943 -1 eustk100 21943 ibutil
procmonUT snoc active 23712 -1 tus100 23712 ibutil
procmonUS snoc active 23796 -1 topsrv1 23796 ibutil
procmonHK snoc active 26602 -1 hibmis100 26602 ibutil
procmonEU snoc active 30129 -1 stopsrv1 30129 ibutil
procmonUI snoc active 32013 -1 usint100 20026 ibutil
procmonUM snoc active 33231 -1 usmrg100 20007 ibutil
procmonFU snoc active 34436 -1 usfrx100 20015 ibutil
procmonEI snoc active 35387 -1 euint100 20019 ibutil
procmonFE snoc active 38268 -1 eufrx100 20024 ibutil
procmonUU snoc active 38377 -1 usstk102 20007 ibutil
procmonNY snoc active 40456 -1 nystk100 20016 ibutil
procmonIL snoc active 43301 -1 cib100 20010 ibutil
procmonSM snoc active 47569 -1 ussmm100 20016 ibutil

casper@casper-laptop:~$ more myfile | perl -ane 'print $F[5]."\n"'
eustk100
tus100
topsrv1
hibmis100
stopsrv1
usint100
usmrg100
usfrx100
euint100
eufrx100
usstk102
nystk100
cib100
ussmm100

what i want to get is
eustk
tus
topssrv
hibmis
stopsrv
usint
usmrg
usfrx
euint
eufrx
euint
eufrx
usstk
nystk
cib
ussmm

i tired appending a regex perl -nle 'print /(\w{1,20})\d{1-20} but it did not work.

casper@casper-laptop:~$ more myfile | perl -ane 'print $F[5]."\n"' | perl -nle 'print /(\w{1,20})\d{1,20}/'
 
Old 08-13-2010, 12:14 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
$ cat casper 
procmonES snoc active 21943 -1 eustk100 21943 ibutil
procmonUT snoc active 23712 -1 tus100 23712 ibutil
procmonUS snoc active 23796 -1 topsrv1 23796 ibutil
procmonHK snoc active 26602 -1 hibmis100 26602 ibutil
procmonEU snoc active 30129 -1 stopsrv1 30129 ibutil
procmonUI snoc active 32013 -1 usint100 20026 ibutil
procmonUM snoc active 33231 -1 usmrg100 20007 ibutil
procmonFU snoc active 34436 -1 usfrx100 20015 ibutil
procmonEI snoc active 35387 -1 euint100 20019 ibutil
procmonFE snoc active 38268 -1 eufrx100 20024 ibutil
procmonUU snoc active 38377 -1 usstk102 20007 ibutil
procmonNY snoc active 40456 -1 nystk100 20016 ibutil
procmonIL snoc active 43301 -1 cib100 20010 ibutil
procmonSM snoc active 47569 -1 ussmm100 20016 ibutil

$ perl -ane '$F[5]=~s/[0-9]//g; print "$F[5]\n"'  casper
eustk
tus
topsrv
hibmis
stopsrv
usint
usmrg
usfrx
euint
eufrx
usstk
nystk
cib
ussmm

Cheers,
Tink

Last edited by Tinkster; 08-13-2010 at 12:18 AM.
 
Old 08-13-2010, 11:41 AM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Tink this is beautiful

so the -n loops through but does not print the output -a splits the string in the fith column into individual characters (ex cib100 = c-i-b-1-0-0) and if a character =~ (matches) a number it substitutes is with a nothing

$ perl -ane '$F[5]=~s/[0-9]//g; print "$F[5]\n"' casper


which part of the script acts on the file first - the print "$F[5]\n"' or the perl -ane '$F[5]=~s/[0-9]//g; ?\
i am just hazy on if the script seperates the string into single characters - how does the script puts the string back together.

Last edited by casperdaghost; 08-13-2010 at 11:48 AM.
 
Old 08-13-2010, 03:53 PM   #4
PradeepKr
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Rep: Reputation: 0
I am afraid this would remove the numbers from within the word also, if any.
eg, euf1rx100 would be printed as eufrx
 
Old 08-13-2010, 05:46 PM   #5
Les Windoze
LQ Newbie
 
Registered: Aug 2010
Location: Florida
Distribution: RHEL5, RHEL6, Solaris 10
Posts: 18

Rep: Reputation: 1
Generic algorithm:
I'd read each line into an array; grab the desired element of the array and
walk the string starting at the end, throwing away values which test true for numbers.
Hit a character and from there forward is the desired result.
 
Old 08-13-2010, 11:59 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by casperdaghost View Post
Tink this is beautiful

so the -n loops through but does not print the output -a splits the string in the fith column into individual characters (ex cib100 = c-i-b-1-0-0) and if a character =~ (matches) a number it substitutes is with a nothing

$ perl -ane '$F[5]=~s/[0-9]//g; print "$F[5]\n"' casper


which part of the script acts on the file first - the print "$F[5]\n"' or the perl -ane '$F[5]=~s/[0-9]//g; ?\
i am just hazy on if the script seperates the string into single characters - how does the script puts the string back together.
Nope regarding the -a ... the array gets split along the
whitespace, and the snippet takes each line, splits the
columns into the array element. $F[5] is column 6.
The regex 's/[0-9]//g' simply strips ANY number from
the 6th column (of every line), and the print then
prints it.


basically -an is shorthand for
Code:
while (<>) {
    @F = split(' ');
}
All the ops on the commandline (following -e) get
executed in sequence AFTER the split.


Cheers,
Tink
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any way to show the permissions with numbers instead of with letters? tirengarfio Linux - Newbie 2 06-03-2010 05:20 PM
How to PRINT a range of letters of a string in Perl resetreset Programming 1 01-27-2009 08:42 AM
[Perl] chop last char from line noir911 Programming 8 02-20-2007 10:16 PM
LXer: Google mixes up letters and numbers LXer Syndicated Linux News 0 10-12-2006 11:54 AM
chop chop, dlink dwl 650 rev M problems? victory! rhoyerboat Linux - Wireless Networking 0 02-08-2005 06:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration