wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Remove unwanted text from a big text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Remove unwanted text from a big text file" Watch "Remove unwanted text from a big text file" New topic
Author

Remove unwanted text from a big text file

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I have a log file which contains a big text lines. I wanted to keep the important lines and remove the unwanted lines from the big text file. I am new in UNIX. I would appreciate if someone can help me write UNIX script to remove the unwanted line from the text file.

Actual Text is like this in the log file:



I wanted the output to be like this:

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

How did you go with Peter's starting suggestions? No point in us rehashing old ground.

Personally, I think that this is a job that calls for Global Regular Expression Print(ing) - grep. Peter's suggestion is closer to what you originally requested, insofar as you would end up with a program that is relatively easy to read and maintain, even by people who don't do much shell scripting. Whereas using grep will result in a more concise script. If you are interested in going the grep route, you may want to look at the Wikipedia article on grep, especially the examples.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

It's worth noting, however, that for the sample message, that the app has had a simple data validation error. For a production app, that should have been intercepted and logged more concisely, since there's no need for a stack trace and there is conversely the possibility that a sufficiently-aware "catch" clause in a Struts action processor might be able to provide more useful context to the message that actually got logged. Assuming that invalid data even needs logging in the first place.


Customer surveys are for companies who didn't pay proper attention to begin with.
Duc Quoc
Greenhorn

Joined: Dec 15, 2011
Posts: 4

Sam Saha wrote:I have a log file which contains a big text lines. I wanted to keep the important lines and remove the unwanted lines from the big text file.
... ... ...
I wanted the output to be like this:


From the shell prompt (console), you can use simple grep command like this: (assumed the log file is "server.log" )



It will filter out all the lines with "at " strings, hence results to your expected text.

To write the output text to a new logfile (says, it's "newfile.log"), simply add like following:


We can also achieve the expected result with sed, awk, perl, vim, ... but I think grep is simple enough :-)

Duc's blog: http://ducquoc.wordpress.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove unwanted text from a big text file
 
Similar Threads
Need help for Shell Script to reduce a file
NoSuchMethodException: does not contain handler
BIRT Exception messge when click to view report
Transport endpoint is not connected
Needs help for grouping data