Equivalent of Substr in Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Equivalent of Substr in Unix
# 1  
Old 12-11-2007
Equivalent of Substr in Unix

Hi,

I have a shell variable which has a value 123456:abcdeg. I want to extract the value which is present before ":". Is there any command in Unix through which I can achieve this. Please suggest.

Thanks,
Saurabh
# 2  
Old 12-11-2007
What I would do is

(a) pipe it through sed to change the : to a space

Code:
VAR=123456:abcdeg
VAR2=`echo $VAR | sed y/:/\ /`

(b) use a function to get the first part

Code:
first()
{
   return $1
}

VAR3=`first $VAR2`

# 3  
Old 12-11-2007
You didn't say which shell you were using, but generally this should work:

Code:
# VAR="abc:123"
# echo ${VAR##*:}
123
# echo ${VAR%%:*}
abc

If that doesn't work, there are plenty of methods for chopping up strings:

Code:
# echo $VAR | awk -F: '{print $1}'
abc
# echo $VAR | sed 's/:.*//'
abc

# 4  
Old 12-11-2007
Hi.

As gus2000 noted, you can do this in some shells. In most systems, expr also has some limited string matching:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate string extraction from variable.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash expr

echo

VAR="123456:abcdeg"
echo " Goal: extract text before : from \"$VAR\""

echo
echo " Old school with expr string matching:"
expr $VAR : "\(.*\):"

echo
echo " bash / ksh parameter expansions:"
echo ${VAR%%:*}
echo ${VAR//:*}

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
expr (GNU coreutils) 5.2.1

 Goal: extract text before : from "123456:abcdeg"

 Old school with expr string matching:
123456

 bash / ksh parameter expansions:
123456
123456

See man pages for details ... cheers, drl
# 5  
Old 12-11-2007
there's also echoing and piping into cut -f1 (or 2) -d':'
# 6  
Old 12-11-2007
Thanks.

Thanks a lot for your inputs.
# 7  
Old 12-11-2007
Hi.

You can also (re)set the argument string-vector:
Code:
echo
echo " Setting the argument vector:"
oldIFS="$IFS"
IFS=":"
set -- $VAR
IFS="$oldIFS"
echo " First argument is \"$1\""

Producing:
Code:
 Setting the argument vector:
 First argument is "123456"

cheers, drl

Last edited by drl; 12-11-2007 at 07:53 PM.. Reason: Quotes were missing around oldIFS
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Linux equivalent for UNIX

I have a folder called "log" which has a few sub-folders say "fda" "fd7" "fdd" "fd6 .... " I wish to fire the below command inside each subfolder starting with the folder with the latest time stamp. grep "$greptime.*exit" Prod.$(hostname).log | grep $fdrdate_new If the seach did not yield... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. HP-UX

UNIX VI editor equivalent of LINUX

Hi All, I am comfortable working in LINUX and need equivalents for HP-UX for below mentioned, 1. We use TAB key to expand/reveal a name in LINUX. Is there any way to make this work for UNIX, where it is double escape. 2. Also can we use make use of left,down,up,right keys instead... (3 Replies)
Discussion started by: pradebban
3 Replies

3. UNIX for Dummies Questions & Answers

Locate equivalent in UNIX

Hi All, I am a frequent user of loacate in linux and really impressed with its speed and accuracy. I would like to know if there is any such equivalent in UNIX. (not the find, which is relatively very slow) Any locate packages which can be made available in UNIX(HP-UX) for this? TIA,... (4 Replies)
Discussion started by: pradebban
4 Replies

4. UNIX for Dummies Questions & Answers

Unix equivalent of DOS set

Hi all, what is the equivalent command of the DOS set that lists all the environment variable and their values? Xavier. (3 Replies)
Discussion started by: xxavier
3 Replies

5. UNIX for Advanced & Expert Users

Equivalent command for setlocal in Unix

Hi all, Is there any command in unix equivalent to setlocal in windows. setlocal command is really useful in restoring local environment variables in windows. Thanks, Sonal. (7 Replies)
Discussion started by: sonaluphale
7 Replies

6. UNIX for Advanced & Expert Users

ufsdump equivalent in linux and unix

Hi all, I am preparing for a worst case scenario. Say i have a production server A, should A fails ( for whatever reason), i want another server B to take over. How can i move everything from A to B? Assuming i have regular backup of A. I've searched in the forums, and briefly came across... (4 Replies)
Discussion started by: new2ss
4 Replies

7. Programming

IsDBCSLeadByteEx equivalent for unix

I am using IsDBCSLeadByteEx for windows, i would like to know whether there is any equivalent function in unix(linux) platform. (2 Replies)
Discussion started by: anjan_kumar_k
2 Replies

8. UNIX for Advanced & Expert Users

IsDBCSLeadByteEx equivalent for unix

I am using IsDBCSLeadByteEx for windows, i would like to know whether there is any equivalent function in unix(linux) platform. (1 Reply)
Discussion started by: anjan_kumar_k
1 Replies

9. UNIX for Advanced & Expert Users

batch file equivalent for Unix

i was trying to do a simple batch file equivalent in Unix when i write a single command in a file, give executable permissions and run it (i gave the file name as a command at the prompt), it works fine. but when i have more than 1 command, say my file has a.out ls ls a.out it doesnt... (1 Reply)
Discussion started by: megastar
1 Replies

10. Programming

unix equivalent for windows APIs..........................

Hi, Is there any unix equivalents available for the folllowing windows function ? FindFirstFile FindNextFile etc..... Or do i have to write an equivalent api?? Can anybody help me to do the same?? thanks in advance Ani (2 Replies)
Discussion started by: ani
2 Replies
Login or Register to Ask a Question