Deleting a list of words from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting a list of words from a text file
# 1  
Old 04-12-2019
Deleting a list of words from a text file

Hello,

I have a list of words separated by spaces I am trying to delete from a text file, and I could not figure out what is the best way to do this.

what I tried (does not work) :
Code:
delete="password key number verify"
arr=($delete)
for i in arr
{
   sed "s/\<${arr[i]}\>[[:space:]]*//g" in.txt 
}
> out.txt

I have hundreds of words I want to delete, and I don't want to put all of them inside the quotes of sed nor inside an external file.
Please any suggestions how to solve the issue? any other efficient ways?
# 2  
Old 04-12-2019
Code:
awk ' {gsub("password|key|number|verify","") } 1 '  in.txt > out.txt

# 3  
Old 04-12-2019
Welcome to the forum.


How do you plan to deliver the "hundreds of words I want to delete" if you "don't want to put all of them inside the quotes of sed nor inside an external file"?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-12-2019
Quote:
Originally Posted by anbu23
Code:
awk ' {gsub("password|key|number|verify","") } 1 '  in.txt > out.txt

anbu23 looks like it is working. the problem is that it is also deleting sub strings and not just the word. in addition, is there any option to delete spaces that come after the deleted word?

say I have a sentence: The password and the key are safe in the vault - please verify.
and after the script I should get : The and are safe in the vault - please.

Quote:
Originally Posted by RudiC
Welcome to the forum.


How do you plan to deliver the "hundreds of words I want to delete" if you "don't want to put all of them inside the quotes of sed nor inside an external file"?
Thank you RudiC, and you are right - that makes no sense. what i meant is that i don't want to manually separate the words with whatever separator is needed - but guess I can just write a script for that.
# 5  
Old 04-12-2019
Code:
awk ' { gsub("\\<(password|key|number|verify)\\> *","") } 1 '  in.txt > out.txt

# 6  
Old 04-14-2019
Quote:
Originally Posted by anbu23
Code:
awk ' { gsub("\\<(password|key|number|verify)\\> *","") } 1 '  in.txt > out.txt

Thank you !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deleting words between every appearance of two words

Hi there, newbie there. I've been browsing the forums hoping to find a solution that answers a problem similar to what I need, but haven't had much luck. Any help would be greatly appreciated. Thanks! I need to delete a bunch of text between every appearance of two words in a really large file... (3 Replies)
Discussion started by: lendl
3 Replies

2. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

3. Shell Programming and Scripting

Search for words NOT in a text file

I have a long list of alphanumberic words (no spaces or characters) in file1.txt I need to check for the existance of each of the words from file1.txt against file2.txt and if the word is NOT in file2.txt, I need to know about it, either standard output or redirect to file3.txt For example:... (5 Replies)
Discussion started by: ajp7701
5 Replies

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

5. UNIX for Dummies Questions & Answers

Deleting Block of Text from a File

Hi I am looking for the way to delete the block of data for example original file line1 line2 line3 line4 line5 input file line2 line3 original file should contain line1 line4 line5 (3 Replies)
Discussion started by: rakeshkumar
3 Replies

6. Shell Programming and Scripting

indexing list of words in a file

Hey all, I'm doing a project currently and want to index words in a webpage. So there would be a file with webpage content and a file with list of words, I want an output file with true and false that would show which word exists in the webpage. example: Webpage content data.html ... (2 Replies)
Discussion started by: Johanni
2 Replies

7. UNIX for Dummies Questions & Answers

deleting words in list with more than 2 identical adjacent characters

Morning Guys & Gals, I am trying to figure out a way to remove lines from a file that have more than 2 identical characters in sequence.. So if for instance the list would look like ; the output would be ; I can't seem to get my head around perl (among many other... (7 Replies)
Discussion started by: TAPE
7 Replies

8. Shell Programming and Scripting

List all file names that contain two specific words.

Hi, all: I would like to search all files under "./" and its subfolders recursively to find out those files contain both word "A" and word "B", and list the filenames finally. How to realize that? Cheers JIA (18 Replies)
Discussion started by: jiapei100
18 Replies

9. Shell Programming and Scripting

how to read all the unique words in a text file

How can i read all the unique words in a file, i used - cat comment_file.txt | /usr/xpg6/bin/tr -sc 'A-Za-z' '/012' and cat comment_file.txt | /usr/xpg6/bin/tr -sdc 'A-Za-z' '/012' but they didnt worked..... (5 Replies)
Discussion started by: aditya.ece1985
5 Replies

10. Shell Programming and Scripting

Deleting text from a file

Hi, In my korn shell script, I want to delete some particular text from a certain file...How can this be done? Is the below right? ed $NAMES << EOF echo "" > /dev/null echo "${x} = " > /dev/null echo "name = " > /dev/null echo "adress = " > /dev/null w q EOF (1 Reply)
Discussion started by: n8575
1 Replies
Login or Register to Ask a Question