Sponsored Content
Top Forums Shell Programming and Scripting Strip leading and numbers from a string. Post 302784943 by Don Cragun on Saturday 23rd of March 2013 05:16:44 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
General::Extended(3)					User Contributed Perl Documentation				      General::Extended(3)

NAME
Config::General::Extended - Extended access to Config files SYNOPSIS
use Config::General; $conf = new Config::General( -ConfigFile => 'configfile', -ExtendedAccess => 1 ); DESCRIPTION
This is an internal module which makes it possible to use object oriented methods to access parts of your config file. Normally you don't call it directly. METHODS
configfile('filename') Set the filename to be used by save to "filename". It returns the current configured filename if called without arguments. obj('key') Returns a new object (of Config::General::Extended Class) from the given key. Short example: Assume you have the following config: <individual> <martin> age 23 </martin> <joseph> age 56 </joseph> </individual> <other> blah blubber blah gobble leer </other> and already read it in using Config::General::Extended::new(), then you can get a new object from the "individual" block this way: $individual = $conf->obj("individual"); Now if you call getall on $individual (just for reference) you would get: $VAR1 = ( martin => { age => 13 } ); Or, here is another use: my $individual = $conf->obj("individual"); foreach my $person ($conf->keys("individual")) { $man = $individual->obj($person); print "$person is " . $man->value("age") . " years old "; } See the discussion on hash() and value() below. If the key from which you want to create a new object is empty, an empty object will be returned. If you run the following on the above config: $obj = $conf->obj("other")->obj("leer"); Then $obj will be empty, just like if you have had run this: $obj = new Config::General::Extended( () ); Read operations on this empty object will return nothing or even fail. But you can use an empty object for creating a new config using write operations, i.e.: $obj->someoption("value"); See the discussion on AUTOLOAD METHODS below. If the key points to a list of hashes, a list of objects will be returned. Given the following example config: <option> name = max </option> <option> name = bea </option> you could write code like this to access the list the OOP way: my $objlist = $conf->obj("option"); foreach my $option (@{$objlist}) { print $option->name; } Please note that the list will be returned as a reference to an array. Empty elements or non-hash elements of the list, if any, will be skipped. hash('key') This method returns a hash(if it is one!) from the config which is referenced by "key". Given the sample config above you would get: my %sub_hash = $conf->hash("individual"); print Dumper(\%sub_hash); $VAR1 = { martin => { age => 13 } }; array('key') This the equivalent of hash() mentioned above, except that it returns an array. Again, we use the sample config mentioned above: $other = $conf->obj("other"); my @blahs = $other->array("blah"); print Dumper(@blahs); $VAR1 = [ "blubber", "gobble" ]; value('key') This method returns the scalar value of a given key. Given the following sample config: name = arthur age = 23 you could do something like that: print $conf->value("name") . " is " . $conf->value("age") . " years old "; You can use this method also to set the value of "key" to something if you give over a hash reference, array reference or a scalar in addition to the key. An example: $conf->value("key", \%somehash); # or $conf->value("key", @somearray); # or $conf->value("key", $somescalar); Please note, that this method does not complain about existing values within "key"! is_hash('key') is_array('key') is_scalar('key') As seen above, you can access parts of your current config using hash, array or scalar methods. But you are right if you guess, that this might become problematic, if for example you call hash() on a key which is in real not a hash but a scalar. Under normal circumstances perl would refuse this and die. To avoid such behavior you can use one of the methods is_hash() is_array() is_scalar() to check if the value of "key" is really what you expect it to be. An example(based on the config example from above): if($conf->is_hash("individual") { $individual = $conf->obj("individual"); } else { die "You need to configure a "individual" block! "; } exists('key') This method returns just true if the given key exists in the config. keys('key') Returns an array of the keys under the specified "key". If you use the example config above you yould do that: print Dumper($conf->keys("individual"); $VAR1 = [ "martin", "joseph" ]; If no key name was supplied, then the keys of the object itself will be returned. You can use this method in foreach loops as seen in an example above(obj() ). delete ('key') This method removes the given key and all associated data from the internal hash structure. If 'key' contained data, then this data will be returned, otherwise undef will be returned. AUTOLOAD METHODS
Another usefull feature is implemented in this class using the AUTOLOAD feature of perl. If you know the keynames of a block within your config, you can access to the values of each individual key using the method notation. See the following example and you will get it: We assume the following config: <person> name = Moser prename = Peter birth = 12.10.1972 </person> Now we read it in and process it: my $conf = new Config::General::Extended("configfile"); my $person = $conf->obj("person"); print $person->prename . " " . $person->name . " is " . $person->age . " years old "; This notation supports only scalar values! You need to make sure, that the block <person> does not contain any subblock or multiple identical options(which will become an array after parsing)! If you access a non-existent key this way, Config::General will croak an error. You can turn this behavior off by setting -StrictObjects to 0 or "no". In this case undef will be returned. Of course you can use this kind of methods for writing data too: $person->name("Neustein"); This changes the value of the "name" key to "Neustein". This feature behaves exactly like value(), which means you can assign hash or array references as well and that existing values under the given key will be overwritten. COPYRIGHT
Copyright (c) 2000-2008 Thomas Linden This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. BUGS
none known yet. AUTHOR
Thomas Linden <tlinden |AT| cpan.org> VERSION
2.04 perl v5.12.1 2010-04-09 General::Extended(3)
All times are GMT -4. The time now is 12:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy