How to remove lines from the text file containing specific words through terminal?


I made a text file called


which contains:

pot 2000

derek 45

snow 55

I want to get user input (a name) on which contact to remove, and delete the entire line containing that name. So far, this is what I've done:

# ... previous code
if int(number) == 5:
print "\n"
newdict = {}
with open('contacts.txt','r') as f:
for line in f:
if line != "\n":
splitline = line.split( )
newdict[(splitline[0])] = ",".join(splitline[1:])
print newdict
removethis = raw_input("Contact to be removed: ")
if removethis in newdict:
with open('contacts.txt','r') as f:
new =
new = new.replace(removethis, '')
with open('contacts.txt','w') as f:

When I enter "pot", I come back to the text file and only "pot" is removed, the "2000" stays there. I tried

new = new.replace(removethis + '\n', '')

as other forums suggested,...

0 0

With this online tool, you can remove lines from a text that meet certain criteria. For example, it allows you to remove all blank lines or lines that contain a particular search term. It is also possible to remove all lines that occur more than once. This is useful, for example, to remove duplicates from a list of numbers.

Remove specific lines from a text

Text to be edited:

Remove the following lines:

Text with lines filtered out:

Instructions for using the tool

First, enter the list to be edited into the input field. The next step is to select the type of lines that are to be removed. The choices are: removing blank lines, remove duplicate lines, remove lines that contain specific text or not. Optionally, you can ignore the case. Finally, simply click on the button labeled "Remove lines" to begin the process.


0 0

word count in termimal

How can we get the number of lines or number of words in a file? The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal.

The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.

To count the number of lines, use “wc” with “l” as

wc -l yourTextFile

To count the number of words, use “wc” with “w” option as

wc -w yourTextFile

And to count the total number of characters, use “wc” with “c” as

wc -m yourTextFile

Using wc with no options will get you the counts of bytes, lines, and words (-c, -l and -w option).

>wc file1.txt 1065 5343 40559 file1.txt

Count words, characters, and lines in multiple files

wc command can take multiple files at the same time and give you the number of words, characters, and lines. To get...

0 0

On Sun, 06 Jan 2008 09:21:33 -0800, Francesco Pietra wrote:

Please, how to adapt the following script (to delete blank lines) to
delete lines containing a specific word, or words?

That's tricky, because deleting lines from a file isn't a simple

operation. No operating system I know of (Windows, Linux, OS X) has a

"delete line" function.

Do you really need to delete the lines in place? It would be much simpler
to leave the original data as-is, and create a new file with just the
lines that aren't deleted.

f=open("output.pdb", "r")
for line in f:
if line:
print line

How to adapt this script:

First, think about what this script does. That is, it goes through each
line, and if the line is not blank, it prints it.

What do you want it to do instead? You want it to print the line if the
line doesn't contain a specific word. So that's the...

0 0

Computer is supposed to help make things easier for us. One simple example is to delete lines from a text file that doesn’t contain a specific keyword. This task is a no brainer but very time consuming and tedious. Recently I have spent some time in compiling a list of websites that has copied and published articles taken from this blog to their website. Although Google does a pretty good job in determining the original publisher, it is still a robot based on a bunch of constantly changing algorithm that can and has made mistakes. Searching for websites that has copied the posts from here is very time consuming, so I have used Copyscape Premium to automatically perform a batch scan on all 2000 articles on this website to track down plagiarism of the content from this blog.

Copyscape Premium finished scanning all 2000 posts in just 10 hours and I was able to export the results to a CSV file for further investigation. There are over 20,000 URLs in the list and I want to...

0 0

How to remove all the lines from the text file containing the words "cat" and "rat"?

grep approach

To create a copy of the file without lines matching "cat" or "rat", one can use grep in reverse (-v) and with the whole-word option (-w).

grep -vwE "(cat|rat)" sourcefile > destinationfile

The whole-word option makes sure it won't match cats or grateful for example. Output redirection of your shell is used (>) to write it to a new file. We need the -E option to enable the extended regular expressions for the (one|other) syntax.

sed approach

Alternatively, to remove the lines in-place one can use sed -i:

sed -i "/\b\(cat\|rat\)\b/d" filename

The \b sets word boundaries and the d operation deletes the line matching the expression between the forward slashes. cat and rat are both being matched by the (one|other) syntax we apparently need to escape with backslashes.

Tip: use sed without the -i operator to test the output of the command...

0 0

Hey, Scripting Guy! I’d like to be able to read through a text file, select the lines that begin with a particular word (like Failure), and then save only those lines back to the same text file. Is there any way to do that?

-- AC

Hey, AC. Hey, AC. For simplicity’s sake, we’re assuming you have a text file that looks similar to this:

Success - Operation succeeded 10/1/2004.
Success - Operation succeeded 10/2/2004.
Failure - Operation failed 10/3/2004.
Success - Operation succeeded 10/4/2004.
Failure - Operation failed 10/5/2004.
Success - Operation succeeded 10/6/2004.
Failure - Operation failed 10/7/2004.
Failure - Operation failed 10/8/2004.

You’d like to have a script read through the file, toss out all the lines that begin with Success, and then save the file, a file which will then hold only information about the operations that failed. In other words, you want the revised file to look like this:

0 0

Click "start" and go to "Run" Type "services.msc" at the prompt which will open the Services window Scroll down the alphabetical list in the right window pane until you come to the entry with the name "Print Spooler" Right-click this entry, then select "stop". This will stop the computer running the process that holds your print queues. Leaving that window open for now, click again on "Start", and then click "My Computer" to open a Windows Explorer window. We've stopped the queue service, now we just have to clear the jam that is already there. To do this we navigate to the print spool folder which is hiding within the Windows folders. Usually Windows is installed on C: drive, but you should be easily able to tell when the Explorer window opens which drive it is on. The usual path to the spool folder is C:\WINDOWS\system32\spool\PRINTERS , but yours may be slightly different. Your windows drive may have another name for instance, but this would be uncommon. So click on your...

0 0

If like me, you have to manipulate huge text files containing thousands of lines of data and you hate trawling through them manually, slowly, one-by-one, line-by-line then boy do I have a few tricks to teach you.

These tricks of mass manipulation of text files will turn your week long, heart-wrenching job into a 5 minute heart-lifting pleasure bordering on the orgasmic [just trying to liven things-up].

I used to export and paste my text files into an Open Office spreadsheet then sort them before cutting and pasting. Doing so used to cut in half the time taken to edit my text files. I even pseudo-shuffled lines of data by typing letters of the alphabet beside each data line then sorting them into groups of like letters (e.g all a’s together, all b’s together…). They were the Dark Years (o.k Weeks ;-) but I’m allowed ) to tell my tale my way; and if I want to make people feel sorry for us lonely, depressed webmasters then I will. I’m glad those days are over.


0 0

These are answers to frequently asked questions on channel #bash on the freenode IRC network. These answers are contributed by the regular members of the channel (originally heiner, and then others including greycat and r00t), and by users like you. If you find something inaccurate or simply misspelled, please feel free to correct it!

All the information here is presented without any warranty or guarantee of accuracy. Use it at your own risk. When in doubt, please consult the man pages or the GNU info pages as the authoritative references.

BASH is a BourneShell compatible shell, which adds many new features to its ancestor. Most of them are available in the KornShell, too. The answers given in this FAQ may be slanted toward Bash, or they may be slanted toward the lowest common denominator Bourne shell, depending on who wrote the answer. In most cases, an effort is made to provide both a portable (Bourne) and an efficient (Bash, where appropriate) answer. If a...

0 0

Check out my other tutorials on the Unix Page, and my

Check my blog

Copyright 1994, 1995 Bruce Barnett and General Electric Company

Copyright 2001, 2013 Bruce Barnett

All rights reserved

You are allowed to print copies of this tutorial for your personal use, and link to this page, but you are not allowed to make electronic copies, or redistribute this tutorial in any form without permission.

Original version written in 1994 and published in the Sun Observer

This section describes C Shell (CSH/TCSH) programming. It covers conditional testing, control loops, and other advanced techniques.

This month begins a tutorial on the bad-boy of UNIX, lowest of the low, the shell of last resort. Yes, I am talking about the C shell. FAQ's flame it. Experts have criticized it. Unfortunately, this puts UNIX novices in an awkward situation. Many people are given the C shell as their default shell. They aren't familiar with it, but they have...

0 0
0 0

UltraEdit is a powerful disk-based text editor, programmer's editor, and hex editor that is used to edit HTML, PHP, javascript, Perl, C/C++, Python, and virtually any other coding/programming language. UltraEdit can handle and edit files in excess of 4 gigabytes. An industry award winning application, UltraEdit includes a free trial period so you can try all of the application's features before purchasing a license.

UltraEdit supports 64-bit file handling on both 32-bit and 64-bit Windows platforms (Windows 7 and later).

Text Editing Features

Disk based text editing - supports editing of large 4GB+ files, minimum RAM used Configurable tab options (multiple tabstop values, use spaces instead of tabs, etc.) Tab/word wrap options based on file type/extension Insert/overstrike mode with caret and status bar indication Paragraph alignment Left, center, right, fill (for fixed pitch fonts) Reformat paragraph (cleans up and beautifies text) Line spacing...
0 0

Related YoLinux Configuration and Admin Tutorials:

Monitoring the system:

Basic command line:

Also see ntpstat, smbstatus, ifstat, prtstat, pidstat, lpstat, mailstat, sar, cifsiostat, ...


List of tools:

Process Management:

Processes execute within their own process environment, they have their own memory, current working directory, priority, process ID, parent process ID and the file access privileges of the user ID under which they execute.

The basic Linux monitoring commands such as pstree and ps -auxw and top will inform you of the processes running on your system. Sometimes a process must be terminated. To terminate a process:

Identify the process: pstree -p
OR ps -auxw
OR top Kill the process: kill killall

This will perform an orderly shutdown of the process....

0 0