Blank out words


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Blank out words
# 1  
Old 10-24-2010
Blank out words

Hi there, folks!

back again for a little help from my friends...

My job right now is to get all the words between[*] , in a sentence, deleted and written to a new file, and the code I use is this:

Code:
awk -F"[][]" '{print $2 > "words"}{$2="[ ]"}1' inputfile > outputfile

But now I find that is does it ok for a sentence containing ONLY one pair of brackets and NOT two or more pairs of brackets.

Example sentence:
Code:
The children play [outside] in the summer, but in the winter they play [inside].

I need to get: The children play [ ] in the summer, but in the winter they play [ ].
and the words blanked out have them sent to a new file.

Could you help me?

Thxx in advance.

Last edited by Scott; 10-24-2010 at 08:13 AM.. Reason: Code tags
# 2  
Old 10-24-2010
Try...
Code:
 
awk -F"[][]" '{for(i=0;++i<=NF;){
  if(i%2==0){
    printf "[";print $i >> "words";printf "]"}
  else{printf $i}
}{printf "\n"}
 }' infile

This User Gave Thanks to malcomex999 For This Post:
# 3  
Old 10-24-2010
Thank you so much malcomx999: mission accomplished. Excellent piece of code!!
Could a further improvement be added?
When a line contains 2 groups of words to blank out, they are appended in different lines to the 'words' file; could they appear on the same line? ;.)
# 4  
Old 10-24-2010
Ofcourse...

Code:
awk -F"[][]" '{for(i=0;++i<=NF;){
  if(i%2==0){
    printf "[";printf (i==(NF-1))?$i "\n":$i "\t" >> "words";printf "]"}
  else{printf $i}
}{printf "\n"}
}' infile


Last edited by malcomex999; 10-24-2010 at 10:38 AM..
# 5  
Old 10-24-2010
Thanks again mate. You really 'made my day'.
Great!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. Shell Programming and Scripting

Split multiple blank, even if it's blank

Hello, I am trying to split : qvipari1 opcmsga OVO Message Agent AGENT,EA Aborted qvipbcarls02 opcmsga OVO Message Agent AGENT,EA Aborted qvipcac3 perfalarm Alarm generator ... (10 Replies)
Discussion started by: cterra
10 Replies

4. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

5. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

6. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

7. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

8. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

9. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

10. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies
Login or Register to Ask a Question