Mac Terminal Commands and Apps To Work With Text Files

You can work with text files using the Terminal and variety of commands and command line apps. You can merge files, search them, sort them, extract information and even edit them directly with Terminal text editors like vi and nano.

Comments: 3 Responses to “Mac Terminal Commands and Apps To Work With Text Files”

    Jasper
    4 years ago

    Thank you-most of this was new to me. Do you know a command to append a text file’s name into itself? Ideally this would be on the same row as the existing text, rather than in a new row as with conCATenate. One of my students has a 100 tiny text files that are systematically named and include only 2 numbers. She needs to end up with a single text file that includes the name of each file on the same line as its 2 numbers (so this would open in Numbers as a 3 column sheet with 100 row).

    4 years ago

    Jasper: A simple shell script would output this.
    for i in *; do cat "$i"; echo "\t$i"; done ;
    This will output it to the Terminal. You can then copy and paste into a text file or directly into Numbers.

    Jasper
    4 years ago

    Thanks Gary, I couldn't get that to work but I found/adapted this:

    grep "" *.txt > agnes.txt

    This looks inside any .txt-extension files ('file1.txt..') and adds their name and their contents (incrementing pairs of numbers) to a new text file called 'agnes.txt'.

    file1.txt:1, 2
    file2.txt:3, 4
    file3.txt:5, 6

Comments Closed.