Inverting positions of variable & pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inverting positions of variable & pattern
# 1  
Old 12-06-2010
Inverting positions of variable & pattern

Hi, Can somebody help me to know how to invert the positions of this construct?
${variable%pattern} to ${pattern%variable} for pattern matching positions.

Example 1
Code:
var=testcase
echo {var%e}
testcas

So I am not sure how to invert variable and pattern so I can do something like this
Example 2
Code:
var=testcase
echo {e%var}
testcas

I appreciate any help, thank you.

Last edited by Franklin52; 12-06-2010 at 05:23 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-06-2010
1. Why do you want to do that?
2. What do you mean with "invert" ?
3. You cannot change the parameter expansion syntax
# 3  
Old 12-06-2010
1. I am working on a small project in my job where we need to run a database for a small program in redhat but we need the home directory of every user, so since we don't want to make the changes manually for so many of them, better do it this way with a small script program.
2. Well, let me give you a simple example of this.
./program /home/user /home
the output would be: user
so I was thinking that may be I could switch it and the input would be like this: ./program /home /home/user and output would be: user.
3. I didn't explain my self very well, thank you.
# 4  
Old 12-07-2010
So if we take /program /home/user /home as an example, the first parameter to the script is /home/user, so that would become variable $1 inside the script. You could use:
Code:
user=${1##*/}

You can read more about this here: Shell Command Language: Parameter Expansion
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 12-08-2010
Thank you Mr. Scrutinizer.

Let me put what I was looking for and the nice solution.
THIS IS THE COMMON WAY.
# rightmatch value pattern

echo -e ${1%$2}
echo
rightmatch value pattern
./rightmatch /home/user /*
/home
--------------------------------------------------
THE WAY THAT I WAS LOOKING FOR WAS THIS
pattern=$1
value=$2

echo -e ${value#$pattern}
echo
$ leftmatch pattern value
./leftmatch /* /home/user
/home

So thank you for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

4. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

5. Shell Programming and Scripting

Finding BEGINNING & ENDING positions of sequentially increasing sublists from a perl numeric array

I have got an Perl array like: @array = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9...............) This numeric sequence will be always sequentially increasing, unless it encounters, The beginning of the new sequentially increasing numeric sequence. SO in this array we get sequentially... (5 Replies)
Discussion started by: teknokid1
5 Replies

6. Shell Programming and Scripting

inverting a binary

Hi , I have a binary in a variable and i want to invert it and store in a new variable. i mean, replace 0 with 1 and vice versa. I tried reading character by character with the below script but it didnt provide me the proper result. #!/bin/bash count=1 var1="00100011" while ] do ... (4 Replies)
Discussion started by: srinivasayedla
4 Replies

7. Shell Programming and Scripting

Pattern matching & storing result in variable

Hi! i'm trying to parse textfiles against a pattern and storing the result in a variable. The strings i want to get are embraced by and can occur several times in one line, so e.g. some text anything else endwhat i have so far: #!/bin/bash for f in $* do exec 3<&0 exec 0<$f ... (2 Replies)
Discussion started by: thoni
2 Replies

8. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies
Login or Register to Ask a Question