Extract string in square bracket


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string in square bracket
# 1  
Old 01-14-2009
Extract string in square bracket

Hi

Input text is
[main] some message [value1] some message [value2] some message [value3]

Expected output is
main value1 value2 value3

Any idea how to above values in square brackets using shell scripting?

many thanks.
# 2  
Old 01-14-2009
Code:
$ echo "[main] some message [value1] some message [value2] some message [value3]" | sed 's/^\[//;s/\][^\[]*\[/ /g;s/\]$//'
main value1 value2 value3
$

This User Gave Thanks to Perderabo For This Post:
# 3  
Old 01-14-2009
Quote:
Originally Posted by Perderabo
Code:
$ echo "[main] some message [value1] some message [value2] some message [value3]" | sed 's/^\[//;s/\][^\[]*\[/ /g;s/\]$//'
main value1 value2 value3
$


thanks Perderabo.

Any idea how to make more generic? I meant that input messages sometimes only have 1 items or more than 5 items.... the objective it to extract all values in square bracket in a given string.

example:
"some message [value1] some mesage [value2] [value3] some message[value4] some message [value5] some message"

output is
value1 value2 value3 value4 value5

many thanks
# 4  
Old 01-14-2009
Here is one way of extracting the required strings using ksh93
Code:
str="some message [value1] some mesage [value2] [value3] some message[value4] some message [value5] some message"

echo ${str} | read -A words

while (( i < ${#words[@]} )); do
   if [[ ${words[i]} =~ \[.*\] ]]; then
      str=${words[i]/*\[/}       # strip left
      output+="${str/\]*/} "     # strip right & append
   fi
   (( i++ ))
done

echo "RESULT: $output"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

2. Shell Programming and Scripting

Doubt with regex to replace square bracket

there is a word "welcome" output should be "welcome\ i am using regsub to add backslash "\" in place where ever i find square brackets (open or close).. But i am not getting it... pls help out.. set a {welcome} set d (5 Replies)
Discussion started by: Syed Imran
5 Replies

3. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Extract text between two square [..] brackets

Hi All, After searching about this, I could find some solutions but I am not sure why it is not working in my case. I have a text file with contents between two square brackets. The text file looks like this: Use tags when you post any code so others can easily read your code. You can... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Shell Programming and Scripting

extract word from bracket - shell

Related to : thread : 34769-removing-duplicate-lines-file.html i want to extract the words in () eg: string1="bla bla (aaa) aha hai (aa)" after processing output i need is : aaa aa (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

Split a string with bracket

Hi, Am trying to split a string with bracket in ksh but it is not splitting it correctly. split("Hello, Name(1), Name(2)", main,","); How do i split correctly? (3 Replies)
Discussion started by: nightrider
3 Replies

8. Shell Programming and Scripting

Group on the basis of common text in the square bracket and sorting

File A 99 >ac >ss >juk 70 >acb >defa 90 >ca 100 >aa >abc >bca 85 >cde 81 >ghi >ghij 87 >def >fgh <ijk 89 >fck >ghij >kill >aa The given output shud be 100 >aa >abc >bca 87 >def >fgh <ijk 89 >fck >ghij >kill >aa (2 Replies)
Discussion started by: cdfd123
2 Replies

9. Shell Programming and Scripting

Expect Script square bracket dollar prompt

Hi Thanks for this amazing forum first, I've been searching answers in it for problems that I've encountered at work. The only problem I haven't been able to find a fix for, is a ever waiting for prompt problem in Expect when encounter a $ prompt. I usually set the timeout to -1 cause the... (2 Replies)
Discussion started by: Ikaro0
2 Replies

10. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies
Login or Register to Ask a Question