Return part of string after X numbers of a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return part of string after X numbers of a character
# 1  
Old 10-10-2011
Return part of string after X numbers of a character

My input strings look something like this:
Code:
/dev/vs/dsk/group/vol

I want to print just "group" out of the line... which is always found between the 4th and 5th "/" in the string.

I figure I have to use awk, sed, or some combination to do this, but I've searched the forums and can't find anything matching what I need to do. Smilie

Can anyone please help me do this in ksh?

Last edited by radoulov; 10-10-2011 at 10:42 AM.. Reason: Code tags.
# 2  
Old 10-10-2011
This could be done:

1. In pure shell (using parameter expansion):

Code:
% s=/dev/vs/dsk/group/vol   
% ss=${s#/*/*/*/*} ss=${ss%/*}
% echo "$ss"                  
group

2. Using an external command (if you find it more readable or if you need to replace the 50th occurrence Smilie):

Code:
ss=$(
  printf %s "$s" |
    cut -d/ -f5
  )

or (if your shell supports here strings - ksh93 supports them, ksh88 doesn't)

Code:
ss=$(
  cut -d/ -f5 <<< "$s"
  )


Last edited by radoulov; 10-10-2011 at 10:57 AM..
# 3  
Old 10-10-2011
THANK YOU! I would've never figured that one out!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Return the list of file name prefix with first 6 character

Good day people, Kindly advice what is the operator/command that I should try out to if I am about to return a list of prefix of my filename with first 6 character. I understand I could use sed to retrieve the first 6 charter itself. but i wonder if there is any aix command allow me to loop... (4 Replies)
Discussion started by: cielle
4 Replies

3. Shell Programming and Scripting

Regex - Return numbers of exactly 8 digits

Hi All I am new to this forum and also regex. I am using bash scripting and have a file like this "0012","efgh","12345678","adfdf", "36598745" "87654321","hijk","lmno" I want the ouput to be 12345678 36598745 87654321 Criteria like this - number - 8 carachters long Please let... (21 Replies)
Discussion started by: buttseire
21 Replies

4. UNIX for Dummies Questions & Answers

Grep to return lines not containing a character

Hello , this is my first topic cause I need your little help:( I got .txt file, and I want to find lines without letter 'a', so im writing: grep "" list.txt (list.txt is the file of course) and i have no idea why it's not working because it shows lines with a. (1 Reply)
Discussion started by: bbqtoss
1 Replies

5. Shell Programming and Scripting

return part of a string in bash

Hi, I would like to return the last part of a string in an array of strings in bash. The array contains in each position the content below: and I would like to return only the last part of each string. The correct result would be: How can I do that in bash using AWK?... (7 Replies)
Discussion started by: anishkumarv
7 Replies

6. Shell Programming and Scripting

3 numbers and return the maximum

Hello, I am doing bash and for that doing excersices. The folowing one asks for 3 numbers and is suppose to return the maximum. An error should be returned if on of the numbers is missing. I managed to solve it. Can you give me other ways or advices. #!/bin/bash #Date: 2010.10.19 #Ecrire un... (5 Replies)
Discussion started by: flash80
5 Replies

7. Shell Programming and Scripting

sed and character return problem

Hi, I have a problem with sed. It doesn't recognize the "\n" character. It substitudes an "n", instead of introducing a new line. This doesn't happend with print $ print "test \n \n" (it deos introduce two lines after hello) (AIX) $ sed s/bc/\n/g test.1 >test.2 $ cat test.1 bcdefg... (3 Replies)
Discussion started by: Santiago
3 Replies

8. Shell Programming and Scripting

Substituting carriage return followed by newline character - HELP

-------------------------------------------------------------------------------- Hi All I have a field being returned from the DB that when opened in Vi shows a ^M before the rest of the field is displayed on the next line. I need it so that the only newline character is the end of the... (14 Replies)
Discussion started by: djkane
14 Replies

9. Solaris

Looking up Sun Part Numbers

Hello, Does anyone know where to get a complete listing of Sun Part numbers? I have a UtraSCSI drive box (PN: 599-2261-01) that I want to get docs on. If anyone can point me in a good direction that would help. Ed (2 Replies)
Discussion started by: edkung
2 Replies

10. Shell Programming and Scripting

Test return character.

Hi, in my korn shell I have this code: typeset -uL1 rc read rc?"Insert Y=Yes (default) or N=No >>" If I press enter without value I wish to set rc=Y. This is my default. This test: if ] then .... Do not work. I hope in your help. Thanks in advance. Regards, Giovanni (3 Replies)
Discussion started by: gio123bg
3 Replies
Login or Register to Ask a Question