Strip leading and numbers from a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strip leading and numbers from a string.
# 1  
Old 03-23-2013
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
# 2  
Old 03-23-2013
PS you have posted 12 post here, you should now that you need to use Code Tags

1)
Code:
echo ${VAR1:1}
SOMETHING103

# 3  
Old 03-23-2013
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
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}}'

This User Gave Thanks to Jotne For This Post:
# 5  
Old 03-23-2013
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))
} '

This User Gave Thanks to Yoda For This Post:
# 6  
Old 03-23-2013
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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 03-24-2013
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

This User Gave Thanks to anbu23 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How Select numbers from a line of text, and remove leading spaces?

I have a text file with a line of text that contains numbers and text formatted into groups. I need to extract the number that can be either 1,2 or 3 digits long. Then write it to a variable, but i need to remove any leading spaces in the number first. I can get the numbers out but how to remove... (12 Replies)
Discussion started by: kcpoole
12 Replies

2. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

3. Shell Programming and Scripting

How to strip off the leading filename from 'wc -l' command

Hi... can anyone please tell how do i strip off the leading filename from the wc -l command.... when i fire command wc -l new1 ... its giving output as 14 new1 i want the output as just '14'... i need to use this value in the calculations in the later part of the script..... (2 Replies)
Discussion started by: swap21783
2 Replies

4. Shell Programming and Scripting

Untar specific directory and strip leading directories

Ok so I know the title was probably confusing so here goes: I have a tarball (gzipped) that has a nested directory structure . For example: my.tar.gz (contents) --- ------ --------- ------------ --------------- ... (2 Replies)
Discussion started by: DC Slick
2 Replies

5. Shell Programming and Scripting

Strip out the string

awk -F"\t" -vOFS="\t" '{print $1"\t-\t-","",$6,$7"\t-"$8"\t-\t-\t"$15}' file.tsv > output.tsv Using the above command how to remove the string www.abc.com from the $7 value. (7 Replies)
Discussion started by: sandy1028
7 Replies

6. Shell Programming and Scripting

Strip a string in sh

I have a list of servers that I need my script to ping however this list also has the env they belong too such as SIT, PRD, warehouse and so on. The break character for each section is : A value in my list would look like this... brutus.grhq.xxx.com:warehouse Where brutus.grhq.gfs.com is... (13 Replies)
Discussion started by: LRoberts
13 Replies

7. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

8. Shell Programming and Scripting

Add leading zeroes to numbers in a file

Hello, I am (trying) to write a script that will check to see how many users are logged on to my machine, and if that number is more than 60 I need to kill off all the oldest sessions that are over 60. So far I have been able to check how many users are on and now I am at the part where I have to... (3 Replies)
Discussion started by: raidzero
3 Replies

9. Shell Programming and Scripting

Need to strip a string

I have a file that looks like this: /home/fred/opt/bin /opt/usr/bin /usr/sbin/var/opt I need a way to chop of everything after the last occurance of the / sign including the /. So the file above will now look like this below. /home/fred/opt /opt/usr /usr/sbin/var I tried using... (6 Replies)
Discussion started by: x96riley3
6 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question