1828625 Members
1608 Online
109983 Solutions
New Discussion

How to truncate a file

 
SOLVED
Go to solution
Terrence
Regular Advisor

How to truncate a file

I have a text file called ADA.txt

I need to delete everything after the line that contains the word outsourced.

How can I do this automatically with a script?

Thanks
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: How to truncate a file

Hi:

> 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...
Hein van den Heuvel
Honored Contributor

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.


Sandman!
Honored Contributor
Solution

Re: How to truncate a file

You could try to leverage the csplit(1) command. You can match the case of the string "outsourced" to a tee. And after the csplit(1) operation the results would be in the file xx00.

# csplit -s infile '/outsourced/+1'
Hein van den Heuvel
Honored Contributor

Re: How to truncate a file

Fwiw, perl 5.8.8 on hpux has implemented the 'truncate' function, so if you really want to do a truncate without writting a 'real program' you could use:

$ perl -e 'open X,"+<",shift; while () { last if /\boutsourced\b/} truncate X,tell X' ADA.txt


Hein.


James R. Ferguson
Acclaimed Contributor

Re: How to truncate a file

Hi (again) Terrence:

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...
Terrence
Regular Advisor

Re: How to truncate a file

Appreciate all help and all solutions.

I can't pre-reject solutions or approaches that I know nothing about.

csplit best fit my need.

Thanks to all who answered.
Hein van den Heuvel
Honored Contributor

Re: How to truncate a file

Terrence,

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.
James R. Ferguson
Acclaimed Contributor

Re: How to truncate a file

Hi:

> 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...
Terrence
Regular Advisor

Re: How to truncate a file

You guys are the Siskel and Ebert of the forum.

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

Hein van den Heuvel
Honored Contributor

Re: How to truncate a file

>> You guys are the Siskel and Ebert of the forum.

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.


James R. Ferguson
Acclaimed Contributor

Re: How to truncate a file

Hi (again) Terrence:

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...