How can we get the character before and after a word in a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we get the character before and after a word in a string?
# 1  
Old 05-07-2012
How can we get the character before and after a word in a string?

Hi friends,
I am working in a korn shell. i want to know the command which gives me the previous one character and next one character of a given keyword in a string?

for exmaple:
input string:
Code:
/bin/dir/folder1/.proc_name^folderone

Keyword:
Code:
proc_name

output :
Code:
.^

Thanks in advance Smilie

Last edited by Scrutinizer; 05-12-2012 at 07:50 AM.. Reason: code tags
# 2  
Old 05-07-2012
Can try:
Code:
$ var='/bin/dir/folder1/.proc_name^folderone'
$ echo "$var" | sed 's/.*\(.\)proc_name\(.\).*/\1\2/'
.^

You can try something like this with parameter expansion if your ksh is modern:

Code:
$ var='/bin/dir/folder1/.proc_name^folderone'
$ str=proc_name
$ i=${var%?"$str"*}
$ echo ${var:${#i}:1}${var:${#i}+${#str}+1:1}
.^

# 3  
Old 05-12-2012
@neutronscot: thansk for your solution.
but the 1st method :
Code:
$ var='/bin/dir/folder1/.proc_name^folderone'
$ echo "$var" | sed 's/.*\(.\)proc_name\(.\).*/\1\2/'
.^

above method works fine if there is atleast one chanracter before the "proc_name" and after the "proc_name" BUT it is not woking fine if there is no character before the procname or/and after the rpocname.
for example:
Code:
 echo "bin/dir/folder1/.proc_name^folderon" | sed 's/.*\(.\)proc_name\(.\).*/\1\2/'

output:
Code:
.^

working fine in above case.

BUT

Code:
echo "proc_name^folderon" | sed 's/.*\(.\)proc_name\(.\).*/\1\2/'

output:
Code:
proc_name^folderon

NOT WORKINg FINE IN THE ABOVE CASE Smilie

anyone please help!!!!

Moderator's Comments:
Mod Comment Please view this link: How to use [code]...[/code] tags

Last edited by Scrutinizer; 05-12-2012 at 07:49 AM..
# 4  
Old 05-12-2012
It is working fine, also in that case, since you did not specify this in your requirements. What output are you looking for in that case, and also when the keyword is at the end?
# 5  
Old 05-12-2012
@Scrutinizer I am not telling that he is wrong. he gave me the solution what i asked.
But now i am facing this issue . and and if the keyword is at the end also , i am not getting the result
# 6  
Old 05-12-2012
Yes, because you did not specify this case. So what are you looking for? Only 1 character in that case?
# 7  
Old 05-12-2012
yes only one character in this case .. two in my previous case and if there are no character before and after the keyword i.e if the var and keyword are same, i want to get null .. Smilie
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

Position of character in word

How can I represent the position of 1 (considering only the 1s after the colon) in the word from field5 and above; counting from right to left. Input: TT-124 06-03-14 08-02-10 FAS CAT1:10 TT-125-1 05-03-14 10-06-08 CAS CAT2:1010 FAT1:10000 TT-125-3 07-03-14 11-02-06 FAS FAT1:1101... (6 Replies)
Discussion started by: aydj
6 Replies

3. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

4. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

5. Shell Programming and Scripting

Delete character from a word

Friends, I'm looking for a command that delete the first tho caractere in a word. Here is an exp : I want to replace "20091001" by "091001" or "replace" by "place" Thx, (13 Replies)
Discussion started by: newpromo
13 Replies

6. Shell Programming and Scripting

Command to parse a word character by character

Hi All, Can anyone help me please, I have a word like below. 6,76 I want to read this word and check if it has "," (comma) and if yes then i want to replace it with "." (dot). That means i want to be changed to 6.76 If the word doesnot contain "," then its should not be changed. Eg. ... (6 Replies)
Discussion started by: girish.raos
6 Replies

7. UNIX for Dummies Questions & Answers

How to delete all character before certain word

Hi, For example, i have a string "123 456 789 abc 111 222 333" and I would like to delete all the characters before abc so that it becomes "abc 111 222 333" how can i do that in unix? using sed? note: I actually don't know how many words/charachters before "abc", so the "cut"... (9 Replies)
Discussion started by: phandy
9 Replies

8. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

9. UNIX for Dummies Questions & Answers

to search for a particular character in a word

Hi I would like to accept in a string from user like username/pwd@dbname suppose the user does not input @ then i should throw an error that @ symbol missing . How to achieve this Thanks in advance Suresh (6 Replies)
Discussion started by: ssuresh1999
6 Replies

10. UNIX for Dummies Questions & Answers

Grep for X character on a word

How can I grep for a certain letter that only shows on the 3rd letter or character. ex: ASGHDY SHTYRD SDTYRD IGIKGD I only want the TY part of the 3rd character so output would only be SHTYRD and DDTYRD - I only want the TY on the 3rd character. THANKS (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question