Command to extract word from a string

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to extract word from a string
# 1  
Command to extract word from a string

Hi All,

I have a word and from that word would like to search for certain set of string, is there any command to do so ?

EX :


from the above word, would like to search for strings set and extract the search string and then do if stmt...


say something like this,


result=grep Com $search_word

and in $result i want the just the extracted word ie, result=Com

# 2  
try something like below

if [ `awk 'BEGIN { print index("Component", "Com") }'` -gt 0 ]
echo "Com"

# 3  
Totally longhand using __builtins__, CygWin bash under Windows Vista...
All done with understandable variables so you can see how it works...
my_string="This is a component catalogue..."
substring="a c"
for subscript in $( seq 0 1 $(( ${#my_string} - 3 )) )
	if [ "${my_string:$subscript:$substring_length}" == "$substring" ]
		echo "Substring ~$substring~ found in ~$my_string~ at position $(( $subscript + 1 ))..."
exit 0

result for string "a c"...
AMIGA:~> cd /tmp
AMIGA:/tmp> dos2unix
dos2unix: converting file to Unix format ...
AMIGA:/tmp> ./
Substring ~a c~ found in ~This is a component catalogue...~ at position 9...
AMIGA:/tmp> _

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #190
Difficulty: Easy
In computer science, a data type is an attribute of data which informs the compiler or interpreter how the programmer intends to use the data.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question on grep command (extract a string)

Dear all, I have a file a.txt like below: 1_234560_A_G_b37 1 2 1 2 2 2 ... 1_35465767_C_T_b37 2 1 1 2 2 2 ... 2_490638010_A_T_b37 1 2 1 2 2 2 ... 10_4567899_T_G_b37 2 2 1 2 2 2 ... ... what I want to do is extracting rows starting with "10_" like : 10_4567899_T_G_b37 2 2 1 2 2... (1 Reply)
Discussion started by: forevertl
1 Replies

2. Shell Programming and Scripting

Extract First and matching word from string in UNIX

Thank you (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

3. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

4. Shell Programming and Scripting

how to extract last word and a number from a string

I have the following script (which I made by my self) #!/bin/bash # add a few empty lines to make it more legible # add a date description on each update interval echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >>... (6 Replies)
Discussion started by: mahirzukic2
6 Replies

5. Shell Programming and Scripting

Using SED to extract a word or string.

I am working with the ksh shell in HP UNIX and I am attempting to extract a word, beginning with a particular string and ending at the first space. for example I want to extract the word or string MS_RECENT_ACTIVITY from the following string " This has been entered in MS_RECENT_ACTIVITY the... (2 Replies)
Discussion started by: simpletech369
2 Replies

6. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

7. UNIX for Dummies Questions & Answers

command to extract sub-string out of file names

I have these files in a directory. It may have more class than the sample below: DEPT_CHEM101LEC_D_20110301.DAT DEPT_CHEM101LAB_D_20110301.DAT DEPT_BIO105LEC_D_20110325.DAT DEPT_BIO105LAB_D_20110325.DAT DEPT_CSC308LEC_D_20110327.DAT DEPT_CSC308LAB_D_20110327.DAT Is there way to extract out... (5 Replies)
Discussion started by: lv99
5 Replies

8. Shell Programming and Scripting

Extract single or multi word string in Cshell

I am using the following code: set LASInputFile = `ls *. | head -1` set COMPLine = `grep -i :COMPANY $LASInputFile` to extract the following line from my input file: COMP. XYZ Public Company :COMPANY NAME I now need to extract the full name of the company which... (15 Replies)
Discussion started by: phudgens
15 Replies

9. Shell Programming and Scripting

perl newbie: how to extract an unknown word from a string

hi, im quite new to perl regexp. i have a problem where i want to extract a word from a given string. but the word is unknown, only fact is that it appears as the second word in the string. Eg. input string(s) : char var1 = 'A'; int var2 = 10; char *ptr; and what i want to do is... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

10. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies

Featured Tech Videos