Results 1 to 5 of 5

Thread: Editing text file with a script help

  1. #1
    Join Date
    Dec 2009
    Beans
    20

    Editing text file with a script help

    Hey,

    I'm trying to write a script that would loop through the files of a directory and add the names of each file to a text file.

    I'm relatively new to writing scripts and have no idea how I would be able to edit the text file from bash.
    Any help would be appreciate.
    Thanks.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Editing text file with a script help

    Quote Originally Posted by bball2601 View Post
    I'm trying to write a script that would loop through the files of a directory and add the names of each file to a text file.
    Could you just do this?

    Code:
    ls -1 > files.txt
    The '>' is called redirection. You can use it to redirect the output of any command to a file. The 'ls -1' command is just a directory listing with one column of filenames.
    Last edited by r-senior; April 2nd, 2013 at 04:42 PM. Reason: Added some explanation ...
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Dec 2009
    Beans
    20

    Re: Editing text file with a script help

    Quote Originally Posted by r-senior View Post
    Could you just do this?

    Code:
    ls -1 > files.txt
    Thanks, that works.

    Just out of curiosity, do you know how I would be able to edit a text file via scripting?
    Would it be by using sed, and if so how?

  4. #4
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Editing text file with a script help

    Yes, sed would often be the tool for editing from a script. For example:

    Code:
    $ cat henry.txt 
    So shaken as we are, so wan with care.
    Find we a time for frightened peace to pant.
    $ sed -i 's/frightened/frighted/' henry.txt 
    $ cat henry.txt 
    So shaken as we are, so wan with care.
    Find we a time for frighted peace to pant.
    There are lots of other things to be done with sed -- regular expressions, global replace, deletions, etc. It's well worth learning a little of even if you only remember how to do simple search/replace as above. Awk, sed and grep are three related tools that are all well worth exploring.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Editing text file with a script help

    If both your questions are interconnected, i.e. you want to prepare a list of files, then make some automated edits on each, the first step is superfluous as sed is able to process several files in one command:
    Code:
    sed -i 's/old/new/' path/to/directory/*

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •