Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Strip leading and numbers from a string.

Shell Programming and Scripting


Tags
sed, string

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-23-2013
tristezo2k tristezo2k is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 11 July 2015, 7:26 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 04:34 PM.. Reason: fixing caps
Sponsored Links
    #2  
Old Unix and Linux 03-23-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 212 Times in 202 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 Unix and Linux 03-23-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 5 December 2016, 9:02 AM EST
Location: Newtown, PA
Posts: 172
Thanks: 12
Thanked 49 Times in 45 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 Unix and Linux 03-23-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 212 Times in 202 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 Unix and Linux 03-23-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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 Unix and Linux 03-23-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 5 December 2016, 4:09 AM EST
Location: San Jose, CA, USA
Posts: 9,725
Thanks: 459
Thanked 3,320 Times in 2,840 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 Unix and Linux 03-24-2013
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 7 November 2016, 6:31 AM EST
Location: Bangalore,India
Posts: 2,152
Thanks: 21
Thanked 159 Times in 150 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:52 PM
How to strip off the leading filename from 'wc -l' command swap21783 Shell Programming and Scripting 2 03-23-2011 04:50 PM
Untar specific directory and strip leading directories DC Slick Shell Programming and Scripting 2 02-21-2011 09:42 PM
Add leading zeroes to numbers in a file raidzero Shell Programming and Scripting 3 06-17-2008 12:06 PM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM



All times are GMT -4. The time now is 10:35 AM.