Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Manipulate string in shell script

Shell Programming and Scripting


Tags
string manipulation, unix shell scripting

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-17-2011   -   Original Discussion by Dev_Sharma987
Dev_Sharma987's Unix or Linux Image
Dev_Sharma987 Dev_Sharma987 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 28 April 2011, 3:24 AM EDT
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old Unix and Linux 04-17-2011   -   Original Discussion by Dev_Sharma987
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 8 May 2018, 4:21 PM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
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 03:34 AM.. Reason: code tags
Sponsored Links
    #3  
Old Unix and Linux 04-17-2011   -   Original Discussion by Dev_Sharma987
palanisvr's Unix or Linux Image
palanisvr palanisvr is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 23 March 2015, 3:30 AM EDT
Location: /home/india
Posts: 105
Thanks: 1
Thanked 14 Times in 14 Posts
Hi

Could you elaborate what exactly you need.
    #4  
Old Unix and Linux 04-17-2011   -   Original Discussion by Dev_Sharma987
frans's Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 1 February 2016, 9:47 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
#!/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#
The Following User Says Thank You to frans For This Useful Post:
Dev_Sharma987 (04-17-2011)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to manipulate contents of clipboard js8765 Shell Programming and Scripting 3 12-05-2010 11:44 AM
Shell script to manipulate a file deo_kaustubh Shell Programming and Scripting 2 08-09-2010 08:41 PM
sed script to manipulate the /etc/passwd file Pauline mugisha Shell Programming and Scripting 2 11-29-2009 06:56 PM
Script to manipulate logfile text rosspaddock Shell Programming and Scripting 11 12-02-2008 09:10 PM
Shell script to manipulate a message count for the same IP @ diff session gobinath Shell Programming and Scripting 1 05-28-2008 03:13 PM



All times are GMT -4. The time now is 11:36 AM.