Help needed: Automating adding text to many files on OSX (Mavericks)

Discussion in 'Mac / Hackintosh' started by trenton, Jan 29, 2015.

  1. trenton

    trenton Kapellmeister

    Joined:
    Mar 26, 2014
    Messages:
    101
    Likes Received:
    56
    Not necessarily an audio issue but could help with an audio application.

    Hope this makes some sense. I'm not sure if I'm going to ask it properly.
    Is there a way on Mac to add a line of text to a non .txt file at a certain point and automate it over hundreds of files? Basically I want to add text to hundreds of different files at a certain line without doing each file manually. Is there a way to automate this process?

    Thanks for reading and for any help.
     
  2.  
  3. fiction

    fiction Audiosexual

    Joined:
    Jun 21, 2011
    Messages:
    1,940
    Likes Received:
    706
    OSX is based on FreeBSD and has many command line tools on-board.
    One solution is using the vi editor which can either jump to a specific string or jump to a specific line number.
    Using vi -s {file with commands} you can automate the process given that you know some shell scripting.
    Dunno if you know how to use vi?

    There are alternatives like awk or sed (search for a specific line then add a line after it) or even simpler,
    use the "head" and "tail" programs to split your file after the line you want to add another, like:

    Say, you want to add a line after line number 42 of your file "temp". (Everything after "#" is a comment)
    head -42 temp >output # creates new file "output" with first 42 lines
    echo "Your-Line-To-Add" >>output # adds one line to file "output"
    tail +43 temp >>output # adds the remaining lines (hence 43, not 42) from "temp" to "output"


    Put this inside a script, iterate over the file names using a "for i in *" loop, replace "temp" by "$i" and "output" by "$i.out" and your script will process all files in the current directory.

    Good luck!
     
  4. trenton

    trenton Kapellmeister

    Joined:
    Mar 26, 2014
    Messages:
    101
    Likes Received:
    56
    Will give it a shot but that looks exactly what I was looking for.
    Thank you very much fiction. I truly appreciate it.
     
Loading...
Similar Threads - Help needed Automating Forum Date
Downsampling Help Needed: Aliasing Prevention Working with Sound Feb 12, 2025
Artwork help needed, artwork / fanart Our Art Jan 14, 2025
Help Needed with Roland Cloud D-50 Presets Turning into Unusuable BAK files Software Jan 7, 2025
Help Needed: Drum Replacing! Software Dec 9, 2024
Help Needed: UVR Models!! Software Dec 6, 2024
Loading...