Linux add line to all files

How To Add Line Numbers To Text Files On Linux

Ever wondered how to add line numbers to the standard output of text files? This brief guide explains how you can add line numbers to a given text file. There are multiple ways for adding line numbers to a file. Here I have covered 6 different methods to do it. I will keep adding more methods if I come across any in future. Better bookmark this guide and come back later to see if there are any additions.

Method 1 — Using ‘nl’ command

The «nl» command is dedicated for adding line numbers to a file. It writes the given file to standard output, with line numbers added. I have a file named file.txt with the following contents.

As you see in the above output, the file has 8 lines, with three empty lines. Let us add the line numbers.

The nl command won’t take empty lines into account. It will only add the numbers to non-blank lines. If you want to number all lines including the blank lines, use -b flag like below.

Also, you can add a symbol/special characters after the numbers. For example, to add dot (.) after the numbers, run:

You may want to align the width of the output. To do so, use -w flag like below.

Method 2 — Using ‘cat’ command

The cat command is used to display the contents of a file. If you want to add numbers to the output of a file, use -n flag like below.

Alternatively, you can pass the standard output to new file like below.

You may also want to get rid of the repeated empty lines.

Method 3 — Using ‘awk’ command

To add line numbers to the output of a file using awk command, run:

As may noticed, I have assigned the starting number as 1 in the BEGIN parameter. You can assign any other starting number of your choice, for example 5, as shown below.

Use the following command if you don’t want to take the blank lines into account:

If you think the above commands are bit difficult to remember, use the following command instead.

If you want to increase the space between the numbers and the text, run:

Method 4 — Using ‘sed’ command

To add line numbers to a standard output of a file using sed command, run:

The sed command has a cool feature that I like the most. We can display a Nth line from a file. For example, to display the 3rd line in a file, run:

Method 5 — Using ‘less’ command

To line number to the standard output of a file using less command, run:

Method 6 — Using ‘grep’ command

The grep command can be used to search for a line that contains a specific line. If you want to add the line numbers to a line that has a specific letter, for example line, run:

Please note that this command will only add the numbers to the lines that contains the search string. Everything else in the given file will be omitted.

And, that’s all. For more details of the above commands refer the man pages. You know now the different methods of adding line numbers to the text files. I hope you find this useful. More good stuffs to come. Stay tuned!

Источник

How to append multiple lines to a file

I am writing a bash script to look for a file if it doesn’t exist then create it and append this to it:

So «line then new line ‘tab’ then text» I think its a sensitive format. I know you can do this:

Читайте также:  Astra linux установка виртуальной машины

But it seems weird since its two lines. Is there a way to append that in this format:

10 Answers 10

If sudo (other user privileges) is needed to write to the file, use this:

Or, if it’s a literal tab that you want (rather than the four spaces in your question):

You can achieve the same effect with echo , but exactly how varies from implementation to implementation, whereas printf is constant.

Another approach is to use tee

A few choice lines from tee ‘s man page:

The tee utility copies standard input to standard output, making a copy in zero or more files.

-a — Append the output to the files rather than overwriting them.

Here is an example to append multiple lines in a file:

SED can append a line to the end of a file like so:

sed -i ‘$ a text to be inserted’ fileName.file
$ selects end of file, the a tells it to append, and after this comes the text that is to be inserted. Then of course the file name.

Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this: find . -name «*.html» -exec sed -i ‘$ a ‘ <> \;

I used the above example to insert the ending html tag that was missing on every html page within a number of directories.

Источник

How to insert a text at the beginning of a file?

So far I’ve been able to find out how to add a line at the beginning of a file but that’s not exactly what I want. I’ll show it with an example:

File content

Result

It’s similar but I don’t want to create any new line with it.

I would like to do this with sed if possible.

16 Answers 16

sed can operate on an address:

What is this magical 1s you see on every answer here? Line addressing!.

If you want to add a line at the beginning of a file, you need to add \n at the end of the string in the best solution above.

The best solution will add the string, but with the string, it will not add a line at the end of a file.

If the file is only one line, you can use:

If it’s more than one line. one of:

I’ve included the latter so that you know how to do ranges of lines. Both of these «replace» the start line marker on their affected lines with the text you want to insert. You can also (assuming your sed is modern enough) use:

to do in-place editing.

Unfortunately, command substitution will remove newlines at the end of file. So as to keep them one can use:

Neither grouping nor command substitution is needed.

To insert just a newline:

You can use cat —

To add a line to the top of the file:

This will work even is the string containing forward slash «/»

Note that on OS X, sed -i

file , fails. However, if you provide a backup extension, sed -i old

file , then file is modified in place while file.old is created. You can then delete file.old in your script.

There is a very easy way:

Hi with carriage return:

PROBLEM: tag a file, at the top of the file, with the base name of the parent directory.

tag the top of file1 with Programming .

SOLUTION 1 — non-empty files:

1s places the text at line 1 of the file.

SOLUTION 2 — empty or non-empty files:

Note that the — in the cat command is required (reads standard input: see man cat for more information). Here, I believe, it’s needed to take the output of the printf statement (to STDIN), and cat that and the file to temp . See also the explanation at the bottom of http://www.linfo.org/cat.html.

I also added -f to the mv command, to avoid being asked for confirmations when overwriting files.

Читайте также:  Программное обеспечение windows сервер

To recurse over a directory:

Note also that this will break over paths with spaces; there are solutions, elsewhere (e.g. file globbing, or find . -type f . -type solutions) for those.

ADDENDUM: Re: my last comment, this script will allow you to recurse over directories with spaces in the paths:

Источник

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file .

So I was wondering: How to add a newline at the end of a file to get rid of those messages?

20 Answers 20

And alternatively for OS X sed :

This adds \n at the end of the file only if it doesn’t already end with a newline. So if you run it twice, it will not add another newline:

To recursively sanitize a project I use this oneliner:

git ls-files -z lists files in the repository. It takes an optional pattern as additional parameter which might be useful in some cases if you want to restrict the operation to certain files/directories. As an alternative, you could use find -print0 . or similar programs to list affected files — just make sure it emits NUL -delimited entries.

while IFS= read -rd » f; do . done iterates through the entries, safely handling filenames that include whitespace and/or newlines.

tail -c1 reads the last char from a file.

read -r _ exits with a nonzero exit status if a trailing newline is missing.

|| echo >> «$f» appends a newline to the file if the exit status of the previous command was nonzero.

so echo «» >> noeol-file should do the trick. (Or did you mean to ask for identifying these files and fixing them?)

edit removed the «» from echo «» >> foo (see @yuyichao’s comment) edit2 added the «» again (but see @Keith Thompson’s comment)

Another solution using ed . This solution only affect the last line and only if \n is missing:

It essentially works opening the file for editing through a script, the script is the single w command, that write the file back to disk. It is based on this sentence found in ed(1) man page:

A simple, portable, POSIX-compliant way to add an absent, final newline to a would be text file:

This approach does not need to read the entire file; it can simply seek to EOF and work from there.

This approach also does not need to create temp files behind your back (e.g. sed -i), so hardlinks aren’t affected.

echo appends a newline to the file only when the result of the command substitution is a non-empty string. Note that this can only happen if the file is not empty and the last byte is not a newline.

If the last byte of the file is a newline, tail returns it, then command substitution strips it; the result is an empty string. The -n test fails and echo does not run.

If the file is empty, the result of the command substitution is also an empty string, and again echo does not run. This is desirable, because an empty file is not an invalid text file, nor is it equivalent to a non-empty text file with an empty line.

Add newline regardless:

Here is a way to check if a newline exists at the end before adding one, by using Python:

The fastest solution is:

Is really fast.
On a medium size file seq 99999999 >file this takes miliseconds.
Other solutions take a long time:

Works in ash, bash, lksh, mksh, ksh93, attsh and zsh but not yash.

  • Does not change file timestamp if there is no need to add a newline.
    All other solutions presented here change the timestamp of file.
  • All solutions above are valid POSIX.
  • If you need a solution portable to yash (and all other shells listed above), it may get a bit more complex:

    The fastest way to test if the last byte of a file is a newline is to read only that last byte. That could be done with tail -c1 file . However, the simplistic way to test if the byte value is a new line, depending on the shell usual removal of a trailing new line inside a command expansion fails (for example) in yash, when the last character in the file is an UTF-8 value.

    Читайте также:  Настройка системных значков windows 10

    The correct, POSIX-compliant, all (reasonable) shells way to find if the last byte of a file is a new line is to use either xxd or hexdump:

    Then, comparing the output of above to 0A will provide a robust test.
    It is useful to avoid adding a new line to an otherwise empty file.
    File that will fail to provide a last character of 0A , of course:

    Short and sweet. This takes very little time as it just reads the the last byte (seek to EOF). It does not matter if the file is big. Then only add one byte if needed.

    No temp files needed nor used. No hardlinks are affected.

    If this test is run twice, it will not add another newline.

    If you just want to quickly add a newline when processing some pipeline, use this:

    it’s also POSIX compliant.

    Then, of course, you can redirect it to a file.

    Provided there are no nulls in input:

    . would suffice to always only append a newline to the tail end of an infile if it didn’t have one already. And it need only read the input file through the one time to get it right.

    At least in the GNU versions, simply grep » or awk 1 canonicalizes its input, adding a final newline if not already present. They do copy the file in the process, which takes time if large (but source shouldn’t be too large to read anyway?) and updates the modtime unless you do something like

    (although that may be okay on a file you are checking-in because you modified it) and it loses hardlinks, nondefault permissions and ACLs etc unless you are even more careful.

    Although it doesn’t directly answer the question, here is a related script I wrote to detect files which do not end in newline. It is very fast.

    The perl script reads a list of (optionally sorted) file names from stdin and for every file it reads the last byte to determine if the file ends in a newline or not. It is very fast because it avoids reading the entire contents of each file. It outputs one line for each file it reads, prefixed with «error:» if some kind of error occurs, «empty:» if the file is empty (doesn’t end with newline!), «EOL:» («end of line») if the file ends with newline and «no EOL:» if the file doesn’t end with newline.

    Note: the script doesn’t handle file names which contain newlines. If you’re on a GNU or BSD system, you could handle all possible file names by adding -print0 to find, -z to sort, and -0 to perl, like this:

    Of course, you’d still have to come up with a way of encoding the file names with newlines in the output (left as an exercise for the reader).

    The output could be filtered, if desired, to append a newline to those files which don’t have one, most simply with

    Lack of a final newline can cause bugs in scripts since some versions of shell and other utilities will not properly handle a missing final newline when reading such a file.

    In my experience, the lack of a final newline is caused by using various Windows utilities to edit files. I have never seen vim cause a missing final newline when editing a file, although it will report on such files.

    Finally, there are much shorter (but slower) scripts which can loop over their file name inputs to print those files which do not end in newline, such as:

    Источник

    Оцените статью