#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    49
    Rep Power
    12

    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.
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Posts
    5,306
    Rep Power
    505
    install cygwin.
    Code:
    bash$ for f in interesting_file_pattern ; do head -5 $f > unused_name ; mv unused_name $f ; done
    You knew that.
    Code:
    # 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
    
    import sys
    
    try:
        n = int(sys.argv[-1])
    except:
        n = -10
    
    first_n_lines = [sys.stdin.readline() for i in range(abs(n))]
    
    if n < 0:
        sys.stdout.write(''.join(first_n_lines))
    else:
        sys.stdout.write(sys.stdin.read())
    rem dos.bat something like this
    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]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    49
    Rep Power
    12
    Cool thanks and yeah I knew about cygwin


    Originally Posted by b49P23TIvg
    install cygwin.
    Code:
    bash$ for f in interesting_file_pattern ; do head -5 $f > unused_name ; mv unused_name $f ; done
    You knew that.
    Code:
    # 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
    
    import sys
    
    try:
        n = int(sys.argv[-1])
    except:
        n = -10
    
    first_n_lines = [sys.stdin.readline() for i in range(abs(n))]
    
    if n < 0:
        sys.stdout.write(''.join(first_n_lines))
    else:
        sys.stdout.write(sys.stdin.read())
    rem dos.bat something like this
    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.
    http://www.brew-monkey.com

IMN logo majestic logo threadwatch logo seochat tools logo