Read Text File Line by Line Shell

There are a couple of quick ways to create a text file from the Linux Control Line or Concluding. Some of them accept been explained in the following article.

one) touch command

This is the nearly standard command to chop-chop create an empty text file. The command is quite unproblematic to type and makes information technology quite easier to create several text files at one time. The commands are as follows:

bear upon filename.txt

As simple as that, just type the word touch followed by the name of the file you similar to requite it, and Voila! you have created an empty text file within of a last. Y'all tin add the file names of the file you would similar to create at once with space in betwixt each filename. The command below creates iii empty files at once using the touch control and you can create every bit many files as yous like.

impact file1.txt file2.txt file3.txt        

touch command

touch command for creating multiple files

2) Standard Redirect Symbol(>)

It is also quite easy to understand the command to create a text file in the terminal with the minimum effort. This works really very well for creating a single text file quickly, but for creating several text files at in one case it becomes a chip tedious. The control is simply using the standard redirect symbol (>) spacebar followed by the file proper name.

> filename.txt        

If you want to create several text files at in one case, then you can add the redirect symbol after the previous filename and concatenation the command repeatedly to create multiple empty files.

> file.txt > file2.txt > file3.txt

The above control creates three empty text files. The redirect symbol is quite time-saving if you lot but want to create a single text file. It gets quite longer than the touch command to create multiple empty text files.

Using the redirect symbol for creating files.

iii) CAT Command

Now, this method is as well quite simple and like shooting fish in a barrel to use. Simply type in True cat with two redirect symbols (>>) and the file name( Information technology is non mandatory to use >> symbols, a user can as well apply > symbol, but if the user types  a pre-existing file by mistake, the existing content in the text file volition be overwritten using a unmarried > symbol). This method is a kind of combination of bear on and the redirect symbol commands. This method is a bit quirky, and so you only prefer using the above 2 commands if you want to create an empty never edited file. If you want to create and type in the text file direct away, by far this is quite a brilliant method. This will save yous time to open up up an editor and the command is also quite piece of cake.

The below control creates an empty nonetheless edited file as it prompts the user to create a text file and type in the file at the same time. And then, if you exercise not want to edit the file, only press CTRL+C and information technology will simply leave and create an empty file.

cat >> file.txt

But, if you would similar to add some text to the file, you lot can type in later on this, like this:

cat >> new.txt This is some text in the file from control line.

To stop editing and saving in the file, simply type CTRL+C, it volition create, save and exit the file. So, this method is quite a fourth dimension-saving method if you want to edit the text file very speedily. The following command will append the text to the pre-existing file. On the other manus, if you use a unmarried redirect symbol(>) information technology volition overwrite the content of the file, then you simply prefer using double redirect symbols for prophylactic reasons.

Using cat command to create the file.

Using true cat control to create and write a file.

iv) Using echo / printf

This is likewise similar to cat command, but information technology is very flexible comparatively. The post-obit command is usually used for printing text on the terminal, but we can also use it to write to a file or make an empty file. The repeat command is used along with the double redirect symbols (single > will besides work) followed by the filename.

echo >> filename.txt

If you lot want to create multiple files at a time, you lot tin can chain upwardly the command as in previous methods.

echo >> file1.txt >> file2.txt >> file3.txt

Nosotros can also add functionality to the echo command to quickly create and write to the text file just like  cat command.

echo -eastward 'This will be the text in the file \n this is the new line' >> file.txt

The higher up command can exist highly customizable as it uses the properties of the echo command to make it quite versatile to write the text in the file, simply using a new line character every time tin can be annoying besides.

using the repeat command to create files.

Using echo command to create and write to a file.

Similar to the echo control, we accept the printf command as well. The print control does the same thing as the repeat control but in a C style rather than shell-way editing.

printf "" >> filename.txt
printf "" >> file1.txt >> file2.txt >> file3.txt

printf "This is some text hither \northward The second line \n The third line" >> file.txt

The print command does some pretty C-similar things, such as the newline character and the variable names can be used every bit well, but that is not for a uncomplicated text file. But still, the printf command can be useful in a lot of cases to edit files on the go.

Using printf command to create files.

Using printf to create and write to files.

5) Any command-line text editor(Vim, nano)

This is the most time-consuming method and not the fastest, yet the method can be useful for Linux beginners. If y'all want to heavily edit a text file, you tin can use command-line text-editors such every bit Vim, nano, and there are other options as well. Only well-nigh people employ nano as information technology is simple to use and quick to go. Vim can also be used but most beginners find information technology hard to use, so nosotros'll stick with nano for this example.

nano filename.txt
vim filename.txt

Nosotros are at present in the nano editor(or vim). You lot can type in the stuff y'all require and simply type CTRL+S to save and CTRL+X to exit. In Vim information technology is a fleck different. We won't make a vim guide here, and then you tin check out the 'Nano text editor in Linux' or 'Getting started with vim' article from geeks for geeks.

Using Nano to create and write files.

Then that wraps upwardly the methods for speedily creating a text file or writing to the file. Each method tin be used differently depending on the situation and the example used. Non every method will be the fastest, all the same these were some of the fastest means to create a Text File Using the Command Line in Linux.

mcclurepriess43.blogspot.com

Source: https://www.geeksforgeeks.org/how-to-create-a-text-file-using-the-command-line-in-linux/

0 Response to "Read Text File Line by Line Shell"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel