Script To Delete File If 1st Line Is Blank.

Discussion in 'Software' started by foogoo, Dec 7, 2017.

  1. foogoo

    foogoo Major "foogoo" Geek

    I have a folder full of text files, some have data in the first line, some do not.
    Any ideas how I can delete the files that do not have any data in the first line?
     
  2. Replicator

    Replicator MajorGeek

    The command to 'delete the file if first line blank' using stream editor in Linux bash shell :
    sed '1{/^$/d}' file
    You could then use Cygwin to run the command in Windows and see if this works?
    Someone may have another solution! ;)
     
  3. foogoo

    foogoo Major "foogoo" Geek

    Will that go through each text file in the directory?
     
  4. Replicator

    Replicator MajorGeek

    If no, to point this command at a particular directory containing the txt files i would try this on a test copy of the folder first :
    sed '1{/^$/d}' file ls -al /(directory name)
    Im no sed expert, but i will play around with this problem on my kali machine and try to find what works!
    This should be fun!

    Edit: Does the directory contain any sub-directories or just the said files?
     
    Last edited: Dec 7, 2017
  5. Replicator

    Replicator MajorGeek

    To enter a directory and modify from the terminal in linux i use the cd (directory name) command so perhaps:
    cd (directory name) enter, then
    sed '1{/^$/d}' file
     
  6. Replicator

    Replicator MajorGeek

    The cd (directory name) command should work in dos also so you would only have to Cygwin sed '1{/^$/d}' file for this to run in dos also.
    How i would get this job done would be to run a distro of Linux in my oracle virtual box (Linux Mint18), then create a network file share with my windows host machine, share the folder and bypass Cygwin altogether.
    Post #5 should then work no problems for all files in the directory (providing they are not in sub directories)
    ;)
     
  7. foogoo

    foogoo Major "foogoo" Geek

    all the files are in one directory. Can I specify only text files? there are a few other types in there?
    Appreciate all the help... Can you explain the script parameter by parameter?
    I'm guessing you are looking at the 1 line , what does ^$ do? , then deleting the file.. is that the just of the code?
     
  8. Replicator

    Replicator MajorGeek

    Sed runs the stream editor, putting the 1 in front with /^$/d encased in {} makes the script delete file (expressed by file) only if the first line if blank. This will work for all files, not just txt.
    You could give the script permission, say, to do same with lines 1 to 5 of the file by using 1,5
    ie. sed '1,5{/^$/d}' file
     
  9. TimW

    TimW MajorGeeks Administrator - Jedi Malware Expert Staff Member

  10. foogoo

    foogoo Major "foogoo" Geek

    C:\Program Files (x86)\GnuWin32\bin>sed sed '1{/^$/d}' file
    sed: -e expression #1, char 3: unterminated `s' command
     
  11. Replicator

    Replicator MajorGeek

    giphy.gif
    Thanks for sharing that....will give it a whirl!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds