Extract Bracketed Words


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract Bracketed Words
# 1  
Old 04-26-2017
Extract Bracketed Words

Hi there, Unixers

I need to extract ALL the words from a text which aresurrounded by square brackets. I am using this piece of code

Code:
sed 's/.*\[\(.*\)\].*/\1/g' inputfile > outputfile

but I only get one word for every paragraph, why?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-26-2017 at 06:17 PM.. Reason: Added CODE tags.
# 2  
Old 04-26-2017
Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, and adequate (representative) sample input and desired output data and the logics connecting the two, to avoid ambiguities and keep people from guessing.

Did you consider grep -o?
# 3  
Old 04-26-2017
This is because of greedy matching. The first .* in regular expression tries to match as much as possible, which is why your code will only find the last match on the line.

Instead you could try something like this, where the dot (any character) gets replaced by zero of more matches of "not a particular character" :

Try for example:
Code:
sed 's/[^[]*\[\([^]]*\)\][^[]*/\1 /g' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-27-2017
Yes...but...

Great solution. but could the output words be shown in new lines or separated by slashes. That would be great!!

Smilie
# 5  
Old 04-27-2017
Try:
Code:
sed 's,[^[]*\[\([^]]*\)\][^[]*,\1/,g
     s,/$,,' file

or, if you want to delete lines from the output for input lines that did not contain any words in square brackets:
Code:
sed -n 's,[^[]*\[\([^]]*\)\][^[]*,\1/,g
	s,/$,,p' file

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-27-2017
Just replace the space in the replacement part with the character of your choice.
One caveat is that GNU sed understands \n , but regular sed needs a hard newline.
GNY sed:
Code:
sed 's/[^[]*\[\([^]]*\)\][^[]*/\1\n/g' file

regular sed.
Code:
sed 's/[^[]*\[\([^]]*\)\][^[]*/\
/g' file

--
If you have GNU sed then you will also have GNU grep which has the -o option as RudiC suggested but it leaves the square brackets:

Code:
grep -o '\[[^]]*]' file

--
Another alternative is awk:
Code:
awk 'NF>1{print $1}' RS=\[ FS=\] file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-27-2017
Don, You Got It

Thanks done, perfect piece of code. Saved me headaches! Smilie

---------- Post updated at 02:10 AM ---------- Previous update was at 02:06 AM ----------

Yeah, thanks too Scrutinizer... This one worked fine.

Code:
awk 'NF>1{print $1}' RS=\[ FS=\] file

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract words before and after a certain word.

I have a sample text file with file name: sample.txt The text file has the following text. this is an example text where we have to extract certain words before and after certain word these words can be used later to get more information I want to extract n (a constant) words before and... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

2. Shell Programming and Scripting

Extract words from a pipe

Hello, Currently, I have this output from my application : ------------------------------------------------- Log viewer/Tmp1 (Jun 29 2011 09:48) ------------------------------------------------- BlalbalbaBlalbalba..Blalbalba..Blalbalba..Blalbalba..Blalbalba..Blalbalba..Blalbalba....... (3 Replies)
Discussion started by: acidoangel
3 Replies

3. UNIX for Dummies Questions & Answers

Extract words to new file

Hi there, Unix Gurus Working with big listings of english sentences for my pupils, of the type: 1. If the boss's son had been , someone would have asked for money by now. 2. Look, I haven't a crime, so why can't you let me go? .... I wondered how to extract the words between brackets in... (7 Replies)
Discussion started by: eldeingles
7 Replies

4. Shell Programming and Scripting

expr command to extract words

how to use expr command to retrieve all the words before the equal sign "=" with shell script (3 Replies)
Discussion started by: 76455
3 Replies

5. Shell Programming and Scripting

[sed] extract words from a string

Hi, One of the scripts creates logs in the format: progname_file1.log.20100312020657 where after file the number could be from 1 to 28 and after log. the date is attached in the format YYYYMMDDHHMISS progname_file<1-28>.log.YYYYMMDDHHMISS. Now I want to discard the .20100312020657... (7 Replies)
Discussion started by: dips_ag
7 Replies

6. Shell Programming and Scripting

extract words from txt using perl

Hi, i will deal with txt file and i want to use perl to extract number of words from this txt ex :if the txt file is a story which contains person names and iwant to extract these names and there is something else that these names which i want perl to extract must match the words (person names) ... (2 Replies)
Discussion started by: eng_shimaa
2 Replies

7. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

8. UNIX for Advanced & Expert Users

How to extract two words at the same time.

Hi, Can anyone please let me know, how to extract two lines at the same time. In specific,I have a file containing list of devices, such as router1 and switch2 below. I want to get all the lines which has "#" and all the lines which has "down" router1#sh ip int br Interface ... (6 Replies)
Discussion started by: Aejaz
6 Replies

9. Shell Programming and Scripting

Extract words before and after a pattern/regexp

Couldn't find much help on the kind of question I've here: There is this text file with text as: Line one has a bingo Line two does not have a bingo but it has a tango Bingo is on line three Line four has both tango and bingo Now I would want to search for the pattern "bingo" in this file... (3 Replies)
Discussion started by: manthasirisha
3 Replies

10. Shell Programming and Scripting

extract words with even numbr of letters

Hello All I need to extract words which are of even number of letters and not greater than 10. Any help?? Thanks, Manish (3 Replies)
Discussion started by: manish205
3 Replies
Login or Register to Ask a Question