How to delete strings that matches a word within the same file?

how to delete multiple lines in notepad++
delete lines in a text file that contain a specific string
notepad++ delete lines starting with string
notepad++ delete lines not containing
microsoft word delete lines containing
notepad++ delete line starting with
regex delete line
notepad++ select lines containing string

I am seeking for a function or script that reads every line and seek for a duplicate or match in the same file to then delete it.

The uniq -u function does half the job. I was trying to use a while read with a grep. Some how it works but for some reason it's outputing twice the strings that it's looking for.

This is the code I have for now to identify the matching word. I did not come up yet with the code to then delete the matching word, but I would have used most likely sed

filename=$1
while read line; do
  grep "$line" $filename
done < $filename

and this is the file I have

JonasB/JB
AZ
LeylaS/LS
JB
AmeliaZ/AZ

The expected result should be:

JonasB/"JB"
"AZ"
LeylaS/LS
"JB"
AmeliaZ/"AZ"

But the result I have is this:

JonasB/"JB"
"AZ"
AmeliaZ/"AZ"
"LeylaS/LS"
JonasB/"JB"
"JB"
"AmeliaZ/AZ"

The outcome I would like for this script would be:

LeylaS/LS

Clarification

The previous example brought some confusion and I apologise for it.

This is other example:

ecar/ElectricCar/EV
ElectricCar
EV/ecar
Car/ICE
Van
Hybrid
Truck/Van/MiniVan/BigCar
ElectricVan/ElectricTruck

So the FS would be /.

The lines 2 and 3 should be deleted, because they match line 1 fields

Line 5 should be deleted with line 7, because it matches the second field

The output I expect to have is:

Car/ICE
Hybrid
ElectricVan/ElectricTruck

Hope this clarifies better the issue.

Use awk instead.

BEGIN { FS = "/" } # / is field separator.
($NF in a) {       # if last field is in a;
  delete a[$NF]    # delete it,
  next
} {                # otherwise;
  a[$NF] = $0      # add it to a.
} END {            # at the end;
  for (b in a)     # print everything in a.
    print a[b]
}

Invocation:

awk -F '/' '($NF in a){delete a[$NF];next} {a[$NF]=$0} END{for(b in a) print a[b]}' file

How to Remove Lines Containing a Word or String in a Text File , In other words, I need to remove lines containing the string “.msn.com” in the To do the opposite of the above — i.e., delete lines that do not contain a word or phrase Click Replace All.Now, Notepad++ replaces all those matching lines with� How do I match and remove (delete) the words “ssh_args=-p 1222” from config file using sed command under Linux or Unix like operating systems? You can use the the substitute sed command changes all occurrences of the “ssh_args=-p 1222”. The same command can be used to delete the required words.

Try this:

nl -nrz -w6 -s " " "$filename" | sort -k2 | uniq -s7 | sort -n | cut -c8-

nl numbers the line (6 digits 1 space, max 1 mio lines). sort sorts beginning from part 2 (ignoring line number). unique removes duplicates ignoring 7 chars (the line numbers). sort sorts in original order. Finally cut removes the line numbers.

remove lines that contain a specific text in a file, will remove the lines with example.com and test.com . From man sed : d Delete pattern space. Be careful not to use the same file for input and output, or it will be truncated before example.com and sed replaces only example.com s with empty string. Your regex should match any line containing example.com or test. com Delete matched search term from a file. Step1: Go to command mode and search mode. Step2: Now search for your term and replace it with nothing:%s/searchterm//g. This will help you to delete all the occurrences of your search term. Let me explain above syntax:%s is for searching for entire file, if you want to search in a particular line we can just use :s.

tried on gnu sed

sed -nE 'G; /^([[:print:]]+\n)(.+\n)*\1/b; h;P' filename

Delete Lines Matching Specific Pattern in a File using VIM, This guide discusses on how to delete lines matching specific pattern want just to delete lines matching a specific pattern or a string in a file. There are quite a number of tools out there that you can use to achieve the same. I have a data file (un-structed messy file) from which I have to scrub specific list of strings (delete strings). Here is what I am doing but with no result: infile = r"messy_data_file.txt" outf

Python: How to delete specific lines in a file in a memory-efficient , Delete a line from a file by specific line number in python. Here Keep track of line number; If line number matches the line number in function argument, then skip that line, else add the line in the To do that we are going to use the same logic i.e. Delete lines from a file that contains a word / sub-string. [!^13] matches any character that is not a paragraph mark. [!^13]@ matches any sequence of characters that are not a paragraph mark. So the expression [!^13]@^13 matches any sequence of characters that are not a paragraph mark, followed by a paragraph mark. The other expression has the same meaning, but for line breaks.

Delete substrings within strings - MATLAB erase, This MATLAB function deletes all occurrences of match in str. If match is a string array or a cell array of character vectors, then erase deletes every occurrence of every element of The str and match arguments do not need to be the same size. MATLAB � Simulink � Student Software � Hardware Support � File Exchange. In the Find what field, type in the word or character that you want to delete. Leave the Replace with field blank. Click the Replace All button. Word will indicate how many replacements were made. Each instance of the character or word that you typed in to the Find what field will be deleted from your document.

Searching and Replacing With vi, To search backward in a file, you can use ? instead of / . To match the beginning of a word, type \< at the beginning of the string; to match the end of a You can use all the special matching characters for searches in search-and- replace. In this post we’re going to explore how to find and remove multiple text strings from within another string. Excel has a great built in function called SUBSTITUTE which allows you to find one bit of text within another text string and substitute it for another bit of text.

Comments
  • in your whileloop you are matching filename content against itself, probably it is not what you want to do.
  • Your question is not fully clear. Does your input file contain quotes (") or not? Do you want to have the output LeylaS/LS because there is no line LS? If there would be a line XY without a corresponding XabcY/XY would you want to have a line XY in the output? Please edit your question for clarification or to add more information.
  • So you want to find duplicates or delete them? Hiw do you define a &quot;word&quot;?
  • @Bodo Sorry for the confusion, no there are no quotes in the original file. I quoted the to show what grep was matching. As you said, the LeylaS/LS should be the output because there is no LS or LeylaS in the file. So if either LS or LeylaS match with LeylaS/LS both of those lines needs to be deleted.
  • @downtheroad Yeah... I am trying to grep a line in the [filename] and compare it with the whole file. Where it seems in this case I am comparing the lines from filename in the file.
  • This worked great for this example. If I want this to work with multiple fields what should I edit? Example: JonasB/JB/JonB JB AmeliaZ/AZ/AmeZ/ZA AmeliaZ AmeZ And this should delete all entries. Sorry if this goes over the main question.
  • @Terrenus actually it does, you should have mentioned about that in your question in first place
  • Thanks for the quick response, unfortunately when I tried to run the command, it started duplicating the file multiple times and did not cut the differences.