- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to truncate a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 04:06 AM
07-05-2007 04:06 AM
I need to delete everything after the line that contains the word outsourced.
How can I do this automatically with a script?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 04:28 AM
07-05-2007 04:28 AM
Re: How to truncate a file
> I need to delete everything after the line that contains the word outsourced.
# perl -pe 'print,last if /\boutsourced\b/i' ADA.txt
...keeps the line containing "outsourced" matching case-insensitively and deletes every line thereafter.
If you want to update inplace, do:
# perl -pi.old -e 'print,last if /\boutsourced\b/i' ADA.txt
...in which case a backup copy will be named "ADA.txt.old" and the original file name will contain the snipped data.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 04:35 AM
07-05-2007 04:35 AM
Re: How to truncate a file
Just create a fresh file?
# awk 'NR==1,/outsourced/{print}' ADA.txt > ADA.new
# mv ADA.new ADA.txt
Perl can readily take care of the intermediary file:
# perl -i -ne 'print; last if /outsourced/' ADA.txt
If you do not want the line with outsourced then it simplyfies to:
# perl -i -pe 'last if /outsourced/' ADA.txt
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 05:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 05:47 AM
07-05-2007 05:47 AM
Re: How to truncate a file
$ perl -e 'open X,"+<",shift; while (
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 05:52 AM
07-05-2007 05:52 AM
Re: How to truncate a file
If you want to reject valid solutions that use a particular tool (Perl, in this case), then *say so* when you post. Of course, others may profit even if you don't choose to do so. Thanks.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 06:00 AM
07-05-2007 06:00 AM
Re: How to truncate a file
I can't pre-reject solutions or approaches that I know nothing about.
csplit best fit my need.
Thanks to all who answered.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 06:01 AM
07-05-2007 06:01 AM
Re: How to truncate a file
The csplit solution Sandman proposed is a fine one. It does howeverleave you to deal with renaming the desired output file and also leaves you will cleaning up the undesiredable output files (xx01, xx02 ...)
The awk solution I suggested does not have that extra file(s) problem and is about as (not) complex.
Yet, Sandmans solution is marked perfect (10 points), mine 'nice try' (3 point).
Maybe I'm reading too much in the assigned points (and no, I do not critically need more points).
The perl -i solutions handle the rename for you.
The perl solution(s) have the added advantage that you can readily speficy a 'word boundary' (\b) in the match such that aboutsourced would not trigger the end. I know, probaly not important for the word 'outsourced' but certainly important for a word such as 'end'.
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 06:08 AM
07-05-2007 06:08 AM
Re: How to truncate a file
> I can't pre-reject solutions or approaches that I know nothing about.
Well, the *first* criteria is does it "work", and they all do. The next criteria is probably performance and we'll leave that for evalulation.
BTW, Sandman's solution *is* elegant.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 06:28 AM
07-05-2007 06:28 AM
Re: How to truncate a file
James got 5 points because he was first to respond. I'm not familiar with perl so I wasn't excited by the answer, but would have used it if no one else had an answer.
I sort of disregarded Hein's answer because the perl was similar to Jame's. I skipped past the awk solution because I'm not good with awk. (although yesterday I was eyeing an awk & sed book at Borders so I know I need to improve my skill.) I also didn't give much attention to the post because too often some people (who shall remain nameless) rehash a previous post looking for easy points. If they expand or clarify then they are okay. Ultimately after reexamining the awk solution I find that it would have been my second choice after the csplit.
I liked the csplit solution because it fell within my ability to understand and expand upon. The fact that it leaves a little detritus isn't an issue because I'm splitting an identically named file every day and mailing out the xx00 file. So I don't have to clean anything up, it's just going to be overwritten. It's also something I can see myself using again.
Thanks again for all the help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 06:40 AM
07-05-2007 06:40 AM
Re: How to truncate a file
Thanks! I'll take that as a compliment :-).
>> I skipped past the awk solution because I'm not good with awk.
Yah but... Dare I suggest that at least you knew about awk, and possibly never even knew
csplit existed, let alone that it could take a match string as split point, not just lines or bytes?
I knew about csplit but did not know/remember/realize the power of the match expression as split point.
This is of course the great value of this forum, even for regular answer givers, that one gets alerted to alternative solutions.
>> some people (who shall remain nameless) rehash a previous post looking for easy points.
I've seen that, but I always look at the timestamps before jumping to conclusion.
If they are within a few minutes, even an hour, then an earlier similar reply might not have existed when the similar contribution was made.
>> If they expand or clarify then they are okay.
Exactly!
> Thanks again for all the help
And thank yiou for taking the time to write some feedback. Appreciated. Truly.
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 06:48 AM
07-05-2007 06:48 AM
Re: How to truncate a file
I'll second all of Hein's remarks!
I too take "Siskel and Ebert of the forum" as a complement. I too appreciate your feedback. I too benefited from Sandman's solution --- he always has very clever ones.
Thanks for *your* explanations and your time.
Regards!
...JRF...