Strip leading and numbers from a string. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Strip leading and numbers from a string.

Shell Programming and Scripting


Tags
sed, string

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-23-2013
tristezo2k tristezo2k is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 25 March 2013, 12:34 PM EDT
Location: Buenos Aires, Argentina
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Strip leading and numbers from a string.

Hello I have two vars loaded with
$VAR1="ISOMETHING103"
$VAR2="COTHERTHING04"

I need to:
1) Strip the first char. Could be sed 's/^.//'
2) The number has it's rules. If it has "hundreds", it needs to be striped.
If it is just two digits it shouldn't.
So, for VAR1 output should be "SOMETHING03".
And for VAR2 output should be "OTHERTHING04".
My main issue here is how can I strip the "hundreds"?
Input would be all caps always.

Thanks in advance,
Sebastian

Last edited by tristezo2k; 03-23-2013 at 03:34 PM.. Reason: fixing caps
Sponsored Links
    #2  
Old 03-23-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 July 2014, 9:30 AM EDT
Posts: 1,038
Thanks: 62
Thanked 216 Times in 204 Posts
PS you have posted 12 post here, you should now that you need to use Code Tags

1)

Code:
echo ${VAR1:1}
SOMETHING103

Sponsored Links
    #3  
Old 03-23-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 26 July 2014, 6:48 AM EDT
Location: Newtown, PA
Posts: 121
Thanks: 7
Thanked 28 Times in 27 Posts
Here's one way using awk:


Code:
nawk '{x=length($0); if (substr($0,x-2,1) ~ "[0-9]") {print substr($0,2,x-4) substr($0,x-1,2) } else {print substr($0,2,x-1)} }' filename


Code:
SOMETHING03
OTHERTHING04

    #4  
Old 03-23-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 July 2014, 9:30 AM EDT
Posts: 1,038
Thanks: 62
Thanked 216 Times in 204 Posts
other awk

Code:
echo ${VAR1:1} | awk '{l=length($0);m=match($0,/[0-9]/)} {if (l-m==2) {print substr($0, 1,m-1)substr($0, m+1,2)} else {print}}'

The Following User Says Thank You to Jotne For This Useful Post:
tristezo2k (03-25-2013)
Sponsored Links
    #5  
Old 03-23-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 July 2014, 11:40 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 Posts

Code:
echo "ISOMETHING103" | awk ' {
        match ($0, /^[A-Z]*/)
        printf substr($0, RSTART + 1, RLENGTH - 1)
        match ($0, /[0-9]*$/)
        print (RLENGTH >= 3 ? substr($0, RSTART + 1) : substr($0, RSTART))
} '

The Following User Says Thank You to Yoda For This Useful Post:
tristezo2k (03-25-2013)
Sponsored Links
    #6  
Old 03-23-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 July 2014, 2:26 PM EDT
Location: San Jose, CA, USA
Posts: 4,161
Thanks: 162
Thanked 1,418 Times in 1,202 Posts
If you're using a 1993 or later version of the Korn shell:

Code:
#!/bin/ksh
VAR1="ISOMETHING103"
VAR2="COTHERTHING04"
printf "Initial VAR1=\"%s\", VAR2=\"%s\"\n" "$VAR1" "$VAR2"
VAR1=${VAR1/?/}
VAR2=${VAR2/?/}
VAR1=${VAR1/%[0-9]([0-9][0-9])/\1}
VAR2=${VAR2/%[0-9]([0-9][0-9])/\1}
printf "  Final VAR1=\"%s\", VAR2=\"%s\"\n" "$VAR1" "$VAR2"

will yield:

Code:
Initial VAR1="ISOMETHING103", VAR2="COTHERTHING04"
  Final VAR1="SOMETHING03", VAR2="OTHERTHING04"

If you're using a recent bash:

Code:
#!/bin/bash
VAR1="ISOMETHING103"
VAR2="COTHERTHING04"
printf "Initial VAR1=\"%s\", VAR2=\"%s\"\n" "$VAR1" "$VAR2"
VAR1=${VAR1/?/}
VAR2=${VAR2/?/}
VAR1=${VAR1/%[0-9][0-9][0-9]/${VAR1:$((${#VAR1} - 2))}}
VAR2=${VAR2/%[0-9][0-9][0-9]/${VAR2:$((${#VAR2} - 2))}}
printf "  Final VAR1=\"%s\", VAR2=\"%s\"\n" "$VAR1" "$VAR2"

produces the same output.
Both of these suggestions use parameter expansions that are extensions that are allowed, but not specified, by the standards.
The Following User Says Thank You to Don Cragun For This Useful Post:
tristezo2k (03-25-2013)
Sponsored Links
    #7  
Old 03-24-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 5:16 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
$ echo $VAR1 | sed 's/.//;s/[0-9]\([0-9]\{2\}\)$/\1/'
SOMETHING03
$ echo $VAR2 | sed 's/.//;s/[0-9]\([0-9]\{2\}\)$/\1/'
OTHERTHING04

The Following User Says Thank You to anbu23 For This Useful Post:
tristezo2k (03-25-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Numbers with leading zeros arijitsaha Shell Programming and Scripting 5 09-18-2012 03:52 PM
How to strip off the leading filename from 'wc -l' command swap21783 Shell Programming and Scripting 2 03-23-2011 03:50 PM
Untar specific directory and strip leading directories DC Slick Shell Programming and Scripting 2 02-21-2011 08:42 PM
Add leading zeroes to numbers in a file raidzero Shell Programming and Scripting 3 06-17-2008 11:06 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 01:24 PM



All times are GMT -4. The time now is 02:27 PM.