regex on first string in a variable.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers regex on first string in a variable.
# 1  
Old 09-03-2008
regex on first string in a variable.

Hi all, this is driving me nuts.
I need to evaluate if a variable in a shell script has a heading s or m character e.g. s92342394 or m9233489 if so then I need to get rid of them. I'm quite familiar with PERL and could do it there in 3 mins but I have not found a decent way to do this in a shell. And I can't use Perl for this example.

Any ideas are highly appreciated.

Thanks a ton

This is how far I got so far....Smilie

#for sem in `ipcs -s |grep dmadmin|awk '{print $2}'`
for sem in 's62464053' '53267' 's23760930' '452629'

do
#print $sem
if [echo $sem | egrep -e 's' ]
then
print $sem

else
echo "keins "
#print $sem
#ipcrm -s $sem
fi
done
# 2  
Old 09-03-2008
Code:
for sem in s62464053 m53267 s23760930 452629; do
  case $sem in
    [sm]* ) printf "${sem#?}\n";;
        * ) printf "$sem\n";;
  esac		
done

With some recent shells (bash, ksh93, zsh):

Code:
for sem in s62464053 m53267 s23760930 452629; do
  [[ "$sem" == [sm]* ]] && 
    printf "${sem#?}\n" || 
	  printf "$sem\n"		
done

Or just:

Code:
sem=(s62464053 m53267 s23760930 452629)
printf "%s\n" "${sem[@]/[sm]}"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Using RegEx with variable within bash if [[ ]]

I stumbled upon a problem, which I simplified to this: There is a list of numbers, stored in variable $LIST, lets use `seq 5 25` for demonstration. There is a number that should be compared against this list. For demonstration I use user input - read VALUE I am trying to compare RegEx... (2 Replies)
Discussion started by: Zorbeg
2 Replies

3. Shell Programming and Scripting

Grep with regex containing one string but not the other

Hi to you all, I'm just struggling with a regex problem and I'm pretty sure that I'm missing sth obvious... :confused: I need a regex to feed my grep in order to find lines that contain one string but not the other. Here's the data example: 2015-04-08 19:04:55,926|xxxxxxxxxx| ... (11 Replies)
Discussion started by: stresing
11 Replies

4. Shell Programming and Scripting

String regex comparisons

Here is the sample code: str1="abccccc" str2="abc?" if ]; then echo "same string" else echo "different string" fi Given that ? implies 0 or 1 match of preceding character, I was expecting the output to be "different string", but I am seeing "same string". Am I not using the... (3 Replies)
Discussion started by: Rameshck
3 Replies

5. UNIX for Dummies Questions & Answers

Regex and variable assignment

Hi there, I really didn't want to have to waste people's time and ask this, but after 2 hours and running out of my own time I've decided to ask.. Basically I have a file in the format: word: other words; more words here; last word word: more words here; last word (etc) x 100 Where the... (3 Replies)
Discussion started by: maximus73
3 Replies

6. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

7. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

8. Shell Programming and Scripting

using regex to get part of a string ?

Hi there, i wonder, is it possible to use regular expressions to partially select a string? I have a bunch of server names which look like this server1z-test server2z2 server45z-primary server13z3 I want to extract up to and including the 'z' in the server name, so for example ... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

9. Shell Programming and Scripting

Perl Regex string opperation

I'm working on a basic log parser in perl. Input file looks like: len: 120713 foo bar file size of: testdir1/testdir1/testdir1/testdir1/testfile0 is 120713Of course there are tens of thousands of lines... I'm trying to compare the len and filesize values. #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: dkozel
2 Replies

10. UNIX for Dummies Questions & Answers

regex in variable

Hello! I have problems with syntax... I want a variable, that contains regex... is it possible? Like $ a='.+->.+' and then use it in index ($0, "a"). I guess it won't work... thank you :) (4 Replies)
Discussion started by: alias47
4 Replies
Login or Register to Ask a Question