Pages

Wednesday, February 20, 2013

Linux/Unix Shell scripting: 10 String manapluation examples

When we are working to automate tasks its very much important to manipulate or work with strings. There are different types of manipulating strings using different string techniques. Below are some of the string manipulation techniques available for us.

1)Exact variable substitution
2)Advanced variable declaration
3)Indirect variable declaration
4)String length calculation
5)Sub Strings
6)String manipulation

Exact variable substitution

Example1: I have a variable VAR1 which is equal to abc but why I want to print along some more characters I am unable to print the variable data along appended data.
VAR1=abc
echo "$VAR1xyz"
No Output for above echo command.
This is because your shell treats what ever characters followed after $ as variable name. It stops variable name association only when it sees a space or ". But here our VAR1 is followed by some other charters xyz which make your shell to think VAR1xyz is a variable. In order to make sure Shell to understand VAR1 as a variable we have to bound the variable name with flower braces as shown below. Now try to execute echo  command as below.

echo "${VAR1}xyz"
Output:
abcxyz

Advanced Variable Declaration


Normally we can declare variables without much problem in Bash as it is independent of variable type. But if you want to check if the variable is defined or not its bit tedious job to check every time whether a variable is defined or not. For this we can use advanced variable declaration techniques as shown below examples.


Example2: If VAR1 is set, use the set value, otherwise use surendra as value to VAR1. Note that this will not set VAR1 to surendra if VAR1 is not set but it will use for that occurrence.

For this example VAR1 and VAR2 values as follows.
VAR1=abc
VAR2=

echo ${VAR1:-surendra}
Output:
abc

echo $VAR1
Output:
abc

echo ${VAR2:-surendra}
Output:
surendra
echo $VAR2
No Output

So remember that :- will not set the variable VAR2 with surendra, but it uses for that instances.

Example3: If VAR1 is set, use already set value, otherwise set VAR1 value as surendra.

For this example VAR1 and VAR2 values as follows.
VAR1=abc
VAR2=

echo ${VAR1:=surendra}
Output:
abc

echo $VAR1
abc
echo ${VAR2:=surendra}
surendra
echo $VAR2
surendra

Note: VAR2 is not set, but with this := option we set VAR2 permanently.

Example4: If VAR1 is set, use surendra. If its not set dont use/set anything to it. In this case actual VAR1 value is not used if set.

For this example VAR1 and VAR2 values as follows.
VAR1=abc
VAR2=

echo ${VAR1:+surendra}
surendra
echo $VAR1
abc
echo ${VAR2:+surendra}
No Output
echo $VAR2
No Output

Example5: If variable is not set throw some error stating that its not set.

echo ${VAR1?"This variable VAR1 is not set"}
Output:
This variable VAR1 is not set

Note: You can customize this error message.

Example6: Assign a variable with a number depending on a condition.

For example assign VAR1 with 67 if 45 is less then 35 else assign 89.

For this example VAR1 is not set
echo $VAR1
No output
Now try below example:
echo $((VAR1= 45<35 67:89="" b="">
Output:
89
echo $VAR1
89

Indirect variable declaration

Example7: How can we assign an variable to another value and get the value of first variable to second variable.Suppose take below example

VAR1="Some data here"
VAR2=VAR1

How can we get VAR1 value by using VAR2? We can achieve this by using  indirect variable declaration. To get VAR1 value in VAR2 us below code

echo ${!VAR2}
or
eval echo "\$$VAR2"
Output:
Some data here

Note: Try to avoid assigning a variable to a variable.

String length calculation

Example8: How to get length of a string/variable. Use below command to get VAR1 length

For this example VAR1 is set to abcxyzabc

echo ${#VAR1}
Output
9

Sub Strings

Below examples deals with sub strings, ie getting part of a string.

Example9: Print VAR1 sub string from 4th character.  This will print VAR1 value from 4th position till the end.

VAR1 value is set to abcxyzabc  for following examples.

echo ${VAR1:3}
Output:
xyzabc

Example9: Print last 4 characters of a string.  This will print last 4 characters of VAR1 value.

echo ${VAR1:(-4)}
Output:
zabc

Example10: Printing characters from 3rd to 7th char. The below code will print VAR1 value from 3rd position till 5 chars from that position, so up to 7th.

echo ${VAR1:2:5}
Output:
cxyza

String manipulation

From next example on words we will see string manipulation. These are bit confusing and have to remember. If we can remember we can reduce the code when writing shell scripts.

Example10: Striping shortest sub string between a and b with in the string from LHS.

echo ${VAR1#a*c}
Output:
xyzabc

Example11: Striping longest sub string between a and b with in the string from LHS

echo ${VAR1##a*c}
Output:
no output

${VAR1%a*c} --Strips shortest values between a to b with in the string from RHS
${VAR1%%a*c} --Strips longest values between a to b with in the string from RHS

${VAR1/xyz/abc} --Replaces one occurence of  xyz with abc in VAR1 string
${VAR1//xyz/abc} --Replaces all occurrences of xyz with abc in VAR1 string

${VAR1/#xyz/abc} --Replaces xyz if its present at start of VAR1
${VAR1/%xyz/abc} --Replaces xyz if its present at end of VAR

0 comments:

Post a Comment