• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove unwanted text from a big text file

 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11943
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Duc Quoc
Greenhorn
Posts: 4
Linux Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic