Extract words to new file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract words to new file
# 1  
Old 09-17-2010
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 [kidnapped], someone would have asked for money by now.
2. Look, I haven't [committed] a crime, so why can't you let me go?
....

I wondered how to extract the words between brackets in every sentence to a new file, so if my file contained 100 sentences I would end up with another file containing the 100 words taken out.
Could also the word taken out be replaced for a blank space, so i could get something like ...[ ]...?

Thanx on advance!
# 2  
Old 09-17-2010
Here is a suggestion:

Code:
sed 's/.*\[\(.*\)\].*/\1/g' sent.txt
sed 's/\(.*\[\).*\(\].*\)/\1\2/g' sent.txt

This User Gave Thanks to jostber For This Post:
# 3  
Old 09-17-2010
Code:
sed 's/.*\[\(.*\)\].*/\1/g' inputfile > outputfile

creates a file containing the words taken out, yeah!
Code:
sed 's/\(.*\[\).*\(\].*\)/\1\2/g' inputfile > outputfile

eliminates the word between brackets leaving a space between them. Oh, yeah!!

Yes, yes, yes!!

Last edited by Franklin52; 09-19-2010 at 01:43 PM.. Reason: Adding code tags
# 4  
Old 09-19-2010
Here is a simple command to get the word in [ ], if you don't care of the format.

Code:
grep -o "\[.*\]" infile
[kidnapped]
[committed]

replace the content in [] with a blank space.

Code:
sed 's/\[.*\]/\[ \]/' infile

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?

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 09-19-2010
Code:
 $ cat file
1. If the boss's son had been [kidnapped], someone would have asked for [ money  ]by now.
2. Look, I haven't [committed] a crime, so why can't [   you            ] let me go?

$ ruby -ne 'puts $_.scan(/\[.*?\]/)' file 
[kidnapped]
[ money  ]
[committed]
[   you         ]

$ ruby -ne 'puts $_.gsub(/\[.*?\]/,"[]")' file
1. If the boss's son had been [], someone would have asked for []by now.
2. Look, I haven't [] a crime, so why can't [] let me go?

sed is greedy
Code:
 
$ sed 's/\[.*\]/\[ \]/' file 
1. If the boss's son had been [ ]by now. 
2. Look, I haven't [ ] let me go?


Last edited by kurumi; 09-19-2010 at 09:49 PM..
This User Gave Thanks to kurumi For This Post:
# 6  
Old 09-19-2010
2 in 1, the output with the words goes to the file "words" and the output without the words goes to "newfile":
Code:
awk -F"[][]" '{print $2 > "words"}{$2="[ ]"}1' file > newfile

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 09-19-2010
Wow, tough competition! Guys, you ARE great! All your solutions work perfectly.
After a couple of days working with your proposed pieces of code, I am thinking of being a bit picky: what if the words taken out were appended at the end of the original file - so as not to end up with two files?

Thanx so much to you all.

PS: congratulations to Franklin52 for such a beautiful and practical piece of code.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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 sed 's/.*\.*/\1/g' inputfile > outputfile but I only get one word for every paragraph, why? Please use CODE tags as required by forum rules! (7 Replies)
Discussion started by: eldeingles
7 Replies

2. UNIX for Beginners Questions & Answers

Shell - Read a text file with two words and extract data

hi I made this simple script to extract data and pretty much is a list and would like to extract data of two words separated by commas and I would like to make a new text file that would list these extracted data into a list and each in a new line. Example that worked for me with text file... (5 Replies)
Discussion started by: dandaryll
5 Replies

3. 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

4. Shell Programming and Scripting

Extract words starting with a pattern from a file

Hi Guys.. I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_' For example, ABC.txt ---- INSERT INTO ABC_DLKFJAL_FJKLD SELECT DISTINCT S,B,C FROM ADF_DKF_KDFJ_IERU8 A, ABC_LKDJFREUE9_FJKDF B WHERE A.FI=B.EI; COMMIT; Output : ABS_DLKFJAL_FJKLD,... (5 Replies)
Discussion started by: Pramod_009
5 Replies

5. Shell Programming and Scripting

I need to extract uique words from text file

Hello programmers, I need to create a list of unique words from a text file using PERL...may i have the code for that please? Thank you (1 Reply)
Discussion started by: alsohari
1 Replies

6. Shell Programming and Scripting

To extract a string between two words in XML file

i need to extract the string between two tags, input file is <PersonInfoShipTo AddressID="446311709" AddressLine1="" AddressLine2="" AddressLine3="" AddressLine4="" AddressLine5="" AddressLine6="" AlternateEmailID="" Beeper="" City="" Company="" Country="" DayFaxNo="" DayPhone="" Department=""... (5 Replies)
Discussion started by: Padmanabhan
5 Replies

7. 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

8. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

9. UNIX for Dummies Questions & Answers

To Extract words from File based on Position

Hi Guys, While I was writing one shell script , I just got struck at this point. I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word. Eg : I like Orange very much. I need to replace... (19 Replies)
Discussion started by: kuttu123
19 Replies

10. 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
Login or Register to Ask a Question