Selecting substring (like SQL Server function)

Hey, geniuses of the world (no--facetious is NOT the word of the day;))!

I was wondering if there's a way to extract a specific portion from a string of characters in UNIX/LINUX. Give me the generic capabilities (assuming they exist) and I'll figure out the small details.

But if you know the unique syntax, I'd prefer to do it in bourne shell and I'm using Linux RedHat Enterprise ver 2.6.9-55.ELsmp (if that makes a diff).

e.g. in SQL Server and I know this is a far cry from Linux, but this is just to demonstrate what I hope to achieve:

they have a substring function:

SELECT SUBSTRING(abcdefgh,4,3)

which produces "def" as a result

abcdefgh = expression, 4 specifies the starting character position, and the three specifies how many characters to extract FROM the starting character position

how, if desired, could I accomplish similar results using UNIX/LINUX?!

Thanks for your time/attention in advance!!

your friendly-neighborhood ProGrammar.:cool:

here is one way to do it in shell. starts at 0

var=abcdefgh
echo ${var:3:3}
1 Like

hey, man--where can I find documentation on this li'l procedure?! I mean, what are the underpinnings--what made that work?! What function of UNIX allowed you to do that? Is it built in to the echo function or can I use the :#:# function with other commands such as printf?

If you have more information or could direct me, that'd be great.

Your solution worked like a charm though; YOU'RE THE MAN!!!

I appreciate it, Frank.

it's built right into the shell. the ways to use it are endless. it's all in the man pages or associated documentation. Grab a shell programming book or read an online tutorial. shell programming is very powerful as you will see. you can solve most common problems with a mix of shell+awk+sed.

lemme rephrase...

what keywords should I research to find more about the use/features/options of the ":#:#" convention??

e.g. does that feature have a name?!

You know variable expansion covers how the shell interprets variables, but if people don't know the term "expansion", they might query for the wrong things. You get my point...

I've read lots of documentation and have never come across that, I'm thoroughly familiar with sed, my awk knowledge needs to catch up though.

Dude, I really appreciate this technique. It'll be very helpful when extracting certain portions of uniformly-formatted strings and storing to variables.

So if you know of a particular name (keyword) for that string:#:# technique, I'd appreciate it.

Hi.

It's a shell construct.

It's described under the "Parameter Expansion" section of the shell's man page. So it's fair to assume that "Parameter Expansion" is a good name for it.

1 Like

All you need to know about playing with strings in bash:

Manipulating Strings

1 Like

Thanks, Jim!

I found this same article and came back here to update anybody else who might be seeking similar information. Good find!!

Thanks to everybody else who provided info too--you guys are awesome!! Good knowledge base here!

echo "abcdef" | ruby -e 'print gets[3,3]'