Removing PATTERN from txt without removing lines and general text formatting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing PATTERN from txt without removing lines and general text formatting
# 1  
Old 09-23-2014
Question Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie.

I'm using the terminal to read a poorly formatted book.
The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages.
It looks ve -- ry much like this.
I was hoping to use
Code:
grep -v " -- " file.txt >> newfile.txt

to clean up the file, but, of course, grep removes whole lines.

How would you go about removing these tokens without removing the whole line?

And, people of the forum do you have any other tips and tricks for formatting text files for better viewing in the terminal without using a text editor?
# 2  
Old 09-23-2014
The obvious simple solution is:
Code:
sed 's/ -- //' file.txt > newfile.txt

Is there some reason why your grep command was appending to newfile.txt instead of overwriting it?
# 3  
Old 09-23-2014
Quote:
Originally Posted by Don Cragun
Code:
sed 's/ -- //g' file.txt > newfile.txt

It is very likely that there will be more than one occurrence of -- per line, thus I added a "g" for global replacement. Just in case.
# 4  
Old 09-23-2014
Thank you!
Also, I was using append >> to create a completely new file with the result.
I'm pretty noob !

How do I add to that Thank count you guys have?
# 5  
Old 09-24-2014
You can use the "thumb up/thanks" button at the bottom right corner for posts that you find useful..
# 6  
Old 09-24-2014
Quote:
Originally Posted by AxeHandle
Thank you!
Also, I was using append >> to create a completely new file with the result.
I'm pretty noob !

How do I add to that Thank count you guys have?
Hi AxeHandle,
In both of the file redirections:
Code:
command > file
command >> file

if file did not exist beforehand, it will be created and the output written to standard output by command will be sent to file. If file did exist, the operator > will cause the current contents to be discarded before the data written by command is written to file while the operator >> will leave the current contents of file unchanged and will append the data written by command to the end of the previous contents of file.

In the bottom right corner of any post that you did not write and that you have not already thanked, there is a button with a hand with a thumb up and the word "Thanks". If you press that button, you will be adding your thanks to the person who submitted that post.

junior-helper,
The way I read the 1st post in this thread, I thought the hyphenation was added when a word was split across a page boundary in the original text of the book. I assumed that there wouldn't be more than one page break on a single line (so the g flag wouldn't be needed), but adding the g flag certainly wouldn't hurt.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing md5sum lines stored in text file

Hello. I'm writing a script where every file you create will generate a md5sum and store it into a text file. Say I create 2 files, it'll look like this in the text file: d41d8cd98f00b204e9800998ecf8427e /helloworld/saystheman d41d8cd98f00b204e9800998ecf8427e /helloworld/test I... (3 Replies)
Discussion started by: batarangs_
3 Replies

2. Shell Programming and Scripting

removing lines without text

How do I remove line that do not contain text, but that do contain tabs? I have tried the command cat file | awk NF but that doesn't work when the lines contain tabs (and spaces). I have also tried: cat file | sed '/^$/d' (9 Replies)
Discussion started by: locoroco
9 Replies

3. UNIX for Dummies Questions & Answers

Removing trailing lines at the end of a text file

How do you remove trailing empty lines at the end of a text file? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Shell Programming and Scripting

Removing all lines prior to the last pattern in a file/stream

Hi all, I didn't find anything that specifically answers this after searching for a bit, so please forgive me if this has been covered before. I'm looking to delete all lines prior to the last occurrence of a string in a file or stream from within a shell script (bash.) A bit of... (4 Replies)
Discussion started by: LivinFree
4 Replies

5. Shell Programming and Scripting

removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line. I've googled for this problem, and most people helping are only interested in... (9 Replies)
Discussion started by: tamale
9 Replies

6. UNIX for Dummies Questions & Answers

removing multiple lines of text in a file

Hi, I'm trying to remove multiple lines of text based off a series of different words and output it to a new file The document contains a ton of data but i want to delete any line that has the following mx1.rr.biz.com or ns2.ri.biz.com i tried using grep -v filename "mx1.rr.biz.com" >... (3 Replies)
Discussion started by: spartan22
3 Replies

7. Shell Programming and Scripting

Script for removing text from a txt file

Hello, So I wanted to write a very simple script to remove some information from a text file and save it as something else. For example I have a text file (let's call it txt) with three rows of numbers: 0 0 1 9 8 7 5 0 6 7 9 0 0 7 9 8 1 1 6 4 0 6 0 0 9 8 4 6 0 9 2 8 1 And I want to... (2 Replies)
Discussion started by: hertingm
2 Replies

8. Shell Programming and Scripting

removing pattern which is spread in multiple lines

I have several huge files wich contains oracle table creation scripts as follows: I would need to remove the pattern colored in red above. Any sed/awk/pearl code will be of much help. Thanks (2 Replies)
Discussion started by: sabyasm
2 Replies

9. Solaris

removing particular lines ending with a .cnt extension in a text file

I have a text file with rows of information (it is basically a ls command information(o/p from ls command)) I need to remove the lines ending with a .cnt extension and keep the lines ending with .zip extension, how to accomplish this. I also only need the date,size and name of the file from every... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

Removing lines in a text file.

Here is my problem I'm hoping you guru's can help me figure out. I have a text file that contains comma delimited columns. What I'm looking to do is see if the 24th column on each row in the file contains a value (not null), and then write/append that line to a different file. I've been... (4 Replies)
Discussion started by: WABonnett
4 Replies
Login or Register to Ask a Question