Selecting substring (like SQL Server function)


 
Thread Tools Search this Thread
Operating Systems Linux Selecting substring (like SQL Server function)
# 1  
Old 09-11-2010
Selecting substring (like SQL Server function)

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

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.Smilie
# 2  
Old 09-11-2010
here is one way to do it in shell. starts at 0


Code:
var=abcdefgh
echo ${var:3:3}


Last edited by frank_rizzo; 09-11-2010 at 12:42 AM.. Reason: correct spelling
This User Gave Thanks to frank_rizzo For This Post:
# 3  
Old 09-11-2010
Computer

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.

Quote:
Originally Posted by frank_rizzo
here is one way to do it in shell. starts at 0


Code:
var=abcdefgh
echo ${var:3:3}

# 4  
Old 09-11-2010
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.
# 5  
Old 09-11-2010
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.
# 6  
Old 09-11-2010
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.
This User Gave Thanks to Scott For This Post:
# 7  
Old 09-12-2010
All you need to know about playing with strings in bash:

Manipulating Strings
This User Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substring Function

I have a file in which there are other file path and names /home/data/abc.txt /home/data/sdf.txt /home/data/sdg.txt how can I get the file names i.e. abc.txt sdf.txt sdg.txt I searched the forum for sed command but it was confusing to me (10 Replies)
Discussion started by: eskay
10 Replies

2. Shell Programming and Scripting

Substring a returned function value

Hello, I have something that should be very simple yet I am losing my head in figuring out how to get it to work: I am calling a function passing a parameter, this will return a particular string, next I want to substring the returned value and break it apart. All of this I want to do on a... (2 Replies)
Discussion started by: gio001
2 Replies

3. Shell Programming and Scripting

Passing sql as parameter to unix function

Hi, I have a function which connects to the db and runs the sql. it works fine when I run it like: function "select empname from emp;" but when I try to pass the sql string to a variable which in turn in fed to the function , it throws error. please advise. Thanks, Arnie. (1 Reply)
Discussion started by: itsarnie
1 Replies

4. Shell Programming and Scripting

Call a pl sql function from unix

hi, I want to know how to call a pl sql function testfunction(param1,..) that returns a value and grab that value in a shell variable. Thnx in advance ---------- Post updated 03-30-10 at 11:58 AM ---------- Previous update was 03-29-10 at 03:49 PM ---------- thnx a lot jim (0 Replies)
Discussion started by: austinhell3_16
0 Replies

5. Shell Programming and Scripting

How to do String manipulations using Substring function in Shell

Hi, I have a scenario to just plug out the file name from the following location path. /opt/project/data/int/holdFiles/csv195687.csv So, how do I get just file name which is "csv195687.csv" from the above line using awk/shell scripting? Can we use indexOf and Substring in awk to get... (7 Replies)
Discussion started by: anilvvnn
7 Replies

6. UNIX for Dummies Questions & Answers

Execute PL/SQL function from Unix script (.sql file)

Hi guys, I am new on here, I have a function in oracle that returns a specific value: create or replace PACKAGE BODY "CTC_ASDGET_SCHED" AS FUNCTION FN_ASDSCHEDULE_GET RETURN VARCHAR2 AS BEGIN DECLARE ASDSchedule varchar2(6); ASDComplete... (1 Reply)
Discussion started by: reptile
1 Replies

7. Shell Programming and Scripting

PL/SQL function from shell script

Hi All, I have a shell script in which i want to invoke PL/SQL function and importantly get the return value from the PL/SQL function. Pls let me know how to go about this. Regards, Krishna (0 Replies)
Discussion started by: KrishnaSaran
0 Replies

8. Shell Programming and Scripting

Substring Function

Just so you know guys, I am a SAP Person and I am very new to UNIX. I need a help on a one line code. In one of our script we are referring to a variable ($PN) which has the value /interfaces/DA1/DEV291/outbound/INVOIC which is being used in ftp command. I am just looking for a command to... (1 Reply)
Discussion started by: sasikumar_l
1 Replies

9. UNIX for Dummies Questions & Answers

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (5 Replies)
Discussion started by: csrazdan
5 Replies

10. Shell Programming and Scripting

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (1 Reply)
Discussion started by: csrazdan
1 Replies
Login or Register to Ask a Question