Home Man
Today's Posts

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

string manipulation, unix shell scripting

👤 Login to reply

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

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


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

Could you elaborate what exactly you need.
# 4  
Old 04-17-2011
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)
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help to manipulate data using script nith_anandan Shell Programming and Scripting 8 11-28-2013 10:55 PM
Manipulate XML File Continous STRING by each Order Line using SHELL Pete.kriya Shell Programming and Scripting 3 08-08-2013 04:11 PM
Shell script to manipulate files vedanta UNIX for Advanced & Expert Users 0 04-17-2013 04:19 PM
nawk script to manipulate txt file ment0smintz Shell Programming and Scripting 15 10-03-2012 02:56 AM
Find string in one file and manipulate other pparthiv Shell Programming and Scripting 3 07-16-2012 02: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 08:37 AM
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
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 10:24 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
Show Password

Not a Forum Member?
Forgot Password?