February 18th, 2014, 10:43 AM
Keep top 5 lines from a file and the same file name
Here is what I want to do...
I have a bunch of text files that I need to go through and keep only the top 5 lines. I need the file name to be the same as it was earlier.
Ex.: file.name - take top 5 lines (delete everything below line 5), then save file as file.name. There are a number of files that this will need to be done on as a batch.
I know this can be done in linux, but I only have windows available, but it does have python, yet I am not a python guy... at all.
Any help would be appreciated.
February 18th, 2014, 11:28 AM
You knew that.
bash$ for f in interesting_file_pattern ; do head -5 $f > unused_name ; mv unused_name $f ; done
rem dos.bat something like this
# head.py taking a single argument, the leading number of lines
# copied from stdin to stdout
# if n is positive then omit the first n lines.
# default: -10
# typical command line use:
# python head.py -5
n = int(sys.argv[-1])
n = -10
first_n_lines = [sys.stdin.readline() for i in range(abs(n))]
if n < 0:
for %%f in (*.py) do python p.py -5 < %%f > %%f.head5
verify the .head5 files
then rename them as the originals if you choose.
Or it may be easier to send them head files to a sub-directory. You probably use this handicapped DOS system at work, same as me.
[/code] are essential for python code and Makefiles!
February 18th, 2014, 01:14 PM
Cool thanks and yeah I knew about cygwin
Originally Posted by b49P23TIvg