Skip to content Skip to sidebar Skip to footer

Write a Program to Read in a Text File and Display the Results to the Terminal.

Unix Tutorial #3: Reading Text Files¶

Notation

Topics covered: File manipulation, redirection, streams, stdin, stdout, stderr

Commands used: cat, less, head, wc

The command line is useful for both viewing and manipulating text files. Manipulation ways editing text - for example, replacing words in text files, or appending text from the control line to the cease of a file (also known as redirection). This is useful for creating scripts, text files containing one or more than commands that are run consecutively. In afterward tutorials, y'all will use these techniques to automate your analyses, which can save enormous amounts of fourth dimension.

You can brandish the contents of a file using the true cat command, which stands for concatenate. Let's say we have a file on our Desktop called myFile.txt, which contains the words one through fifteen (i.eastward., i, two, three…15), with each number on a separate line. Use the control line to navigate to the Desktop, and so type true cat myFile.txt . This will print the contents of the file to your command line. This is the same thought as using the GUI to double-click on the text file to see its contents.

../_images/Cat_File.png

Using the command line and the GUI to read the contents of a text file. On the left is the command line using the cat command, which prints the contents to the Last. On the correct is the contents of the file displayed afterward using the mouse to double-click the file.

We refer to the output from this command as stdout, or standard output. The commands that are typed into the Terminal are called stdin, or standard input. This touches on the concept of streams, or the menstruum of information into and out of the command line, and we will use these ideas to give united states more than flexibility in manipulating text files. For at present, think of stdin equally anything you blazon into the Concluding, and stdout as what is returned if the command is run without any errors. If the command that you blazon does result in an mistake - for case, because the command was misspelled or because not enough arguments were provided - the text that is output to the Concluding is called stderr, or standard error.

../_images/Streams.png

Illustration of streams in Unix. Whatever is typed into the Terminal is stdin, and, if it runs without error, whatever is output is chosen stdout. If in that location is an error, the output is instead called stderr.

The true cat command is useful for viewing the contents of smaller files, but if the file contains hundreds of lines of text, it is overwhelming to take everything printed to the Last at once. To encounter only a office of the file, we can utilise the commands caput and tail to encounter the first few or the concluding few lines of the file, respectively. Using myFile.txt every bit an example, typing

Would return the beginning five lines; whereas typing

Would return the last five lines. Although the default is to return five lines, these commands have an option to display any amount of lines that you choose. For example,

                        head            -            x            myFile            .            txt            tail            -            10            myFile            .            txt          

Would render the outset x lines and the last x lines. Try these out yourself, changing the number of lines that are displayed.

Redirection¶

In improver to displaying the results of a control, stdout can be used to move or append the output to a file, a concept known as redirection. For example, if you lot type

The word "xvi" goes into the file tmp.txt instead of being written to standard output. Discover that it creates the file tmp.txt even if it doesn't exist. Notwithstanding, if we endeavour that again with another cord - for example,

It will overwrite the file with whatsoever we printed to standard output. If you want to suspend standard output to the stop of a file without overwriting the other data in the file, use two greater-than signs. For case, blazon

If you blazon cat tmp.txt , yous will see both seventeen and eighteen.

Although these examples are trivial, redirection is invaluable for quickly editing text files and for writing scripts, which allow you to run analyses for hundreds or thousands of subjects with only a few lines of code.

Video¶

Click here for a video walkthrough of commands for reading text files. This video will also show you how to read assistance files using the less command and a paging window.


Exercises¶

  1. Create a new file called "tmp.txt" and type whatever you want into the file. Use true cat to string together both the myFile.txt and tmp.txt files, and redirect the output to create a new file. Print the contents of the new file to stdout.
  2. If you accept AFNI installed on your automobile, use less on the command 3dcalc to find strings matching "Instance." Now effort information technology using the less command with an choice to ignore whether the messages in the string are upper example or lower instance. Hint: To observe this option, search for the string "instance" in the man file for less . (If y'all take FSL installed instead of AFNI, try the aforementioned practice with the command fslmerge .)
  3. Unix has a born control called sort which will sort text numerically or alphabetically. What happens when you use myFile.txt as an argument for sort ? What almost typing this command:

In your ain words, explain the divergence between the ii methods.

banksfunge1999.blogspot.com

Source: https://andysbrainbook.readthedocs.io/en/latest/unix/Unix_03_ReadingTextFiles.html

Post a Comment for "Write a Program to Read in a Text File and Display the Results to the Terminal."