Manipulate string in shell script

Tags
shell scripts, string manipulation, unix shell scripting

 
Thread Tools Search this Thread
# 1  
Old 04-17-2011
Manipulate string in shell script

I am writing a shell script for some purpose. I have a variable of the form -- var1 = "policy=set policy"

Now I need to manipulate the variable var to get the string after index =. that is i should have "set polcy". Also I need to to this for many other variables where the value of "=" is not constant. Like

var2 = "bgroup = set bgroup port"
var3 = "utm = set security utm"
Please give an idea how to do it.
# 2  
Old 04-17-2011
You can use the cut command for this

Code:
var1="policy=set policy"
var1value=`echo $var1 | cut -d"=" -f2`

Note : There should not be any space between the shell variable and it assignment value.

var1 = something => incorrect
var1=something => correct

regards,
Ahamed

Last edited by ahamed101; 04-17-2011 at 04:34 AM.. Reason: code tags
# 3  
Old 04-17-2011
Hi

Could you elaborate what exactly you need.
# 4  
Old 04-17-2011
#!/bin/bash
#
bash code:
  1. #!/bin/bash
  2. var2="bgroup = set bgroup port"
  3. var3="utm = set security utm"
  4. echo "$var2 --> '${var2# = }'"
  5. echo "$var3 --> '${var3# = }'"
  6. # You can also
  7. for V in var2 var3; do
  8.     echo "${!V} --> '${!V# = }'"
  9. done#
This User Gave Thanks to frans For This Post:
Dev_Sharma987 (04-17-2011)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
How to manipulate string in line? baris35 Shell Programming and Scripting 8 10-02-2018 06:04 PM
Need help to manipulate data using script nith_anandan Shell Programming and Scripting 8 11-28-2013 11:55 PM
Manipulate XML File Continous STRING by each Order Line using SHELL Pete.kriya Shell Programming and Scripting 3 08-08-2013 05:11 PM
Shell script to manipulate files vedanta UNIX for Advanced & Expert Users 0 04-17-2013 05:19 PM
nawk script to manipulate txt file ment0smintz Shell Programming and Scripting 15 10-03-2012 03:56 AM
How to get the last value from a string in shell script? vel4ever Shell Programming and Scripting 2 08-01-2012 09:24 AM
Find string in one file and manipulate other pparthiv Shell Programming and Scripting 3 07-16-2012 03:09 AM
Comparing a String variable with a string literal in a Debian shell script daveu7 UNIX for Dummies Questions & Answers 1 03-14-2012 09:37 AM
Writing a loop to manipulate a script and store it in multiple output files evelibertine UNIX for Dummies Questions & Answers 4 10-05-2011 06:40 PM
TRIM a string in shell script Ananthdoss Shell Programming and Scripting 6 10-05-2011 08:56 AM
Script to manipulate contents of clipboard js8765 Shell Programming and Scripting 3 12-05-2010 12:44 PM
Shell script to manipulate a file deo_kaustubh Shell Programming and Scripting 2 08-09-2010 09:41 PM
sed script to manipulate the /etc/passwd file Pauline mugisha Shell Programming and Scripting 2 11-29-2009 07:56 PM
Script to manipulate logfile text rosspaddock Shell Programming and Scripting 11 12-02-2008 10:10 PM
Shell script to manipulate a message count for the same IP @ diff session gobinath Shell Programming and Scripting 1 05-28-2008 04:13 PM