Add end line linux

How to add lines to end of file on Linux

go ro the lines end and add something or the other case When bash finds the comment go down by 2 and than go to the end of the line and add something You see I’m a beginner and don’t have any Idea what to use here and how. Question: I want to add a new column containing only ‘2’ as a value in each row at the end of the a .txt file and save new file as output.

How to add lines to end of file on Linux

I want to add the following 2 lines:

VNCSERVERS="1:root" VNCSERVERARGS[1]="-geometry 1600x1200" 

to the end of the file vncservers found at the directory /etc/sysconfig/ .

The easiest way is to redirect the output of the echo by >> :

echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile 

Append Lines to a File in Linux, This will add two lines to the end of file.txt. 5. The tee Command Another interesting and useful Bash command is the tee command. It reads data from standard input and writes it to the standard output and to files: tee [OPTION] [FILE (s)] tee file1.txt tee file1.txt file2.txt

Find a line in a file and add something to the end of the line in bash

How can I identify a line start with a special pattern and add something to the end of the line?

if the pattern that should be added is not already appended

Let’s say I’d like to find a specific line in the host file either by the pattern at the beginning may be an ip-address or by the comment that is above the line

#This is your hosts file 127.0.0.1 localhost linux #This is added automatically 192.168.1.2 domain1. com #this is added automatically to 192.168.1.2 sub.domain1.com www.domain1.com 

How do I tell bash when you find the IP I tell you. go ro the lines end and add something

When bash finds the comment #This is added automatically

go down by 2 and than go to the end of the line and add something

You see I’m a beginner and don’t have any Idea what to use here and how. Is dis done by sed? or could this be done with grep? do I have to learn AWK for that stuff?

This will add some text to the end of the line with a pattern «127.0.0.1».

grep -F "127.0.0.1" file | sed -ie 's/$/& ADDing SOME MORE TEXT AT THE END/g' 

Following will add to the line in the file that begins with 127.0.0.1 via sed :

sed -ie 's/^127.0.0.1.*$/& ADDing MORE TEXT TO THE END/g' file 

TO do the same, you can also use awk :

awk '/^127.0.0.1/' file > newfile && mv newfile file 

If you want to call the IP address through a variable then syntax might be a bit different :

var="127.0.0.1" grep -F "$var" file | sed -ie 's/$/& ADD MORE TEXT/g' sed -ie "s/^$var.*$/& ADD MORE TEXT/g" file awk '/^'$var'/' file > newfile && mv newfile file 
[root@yourserver ~]# cat text.log #This is your hosts file 127.0.0.1 localhost linux [root@yourserver ~]# 
[root@yourserver ~]# cat so.sh #!/bin/bash _IP_TO_FIND="$1" # sysadmin 101 - the sed command below will backup the file just in case you need to revert _BEST_PATH_LINE_NUMBER=$(grep -n "$" text.log | head -1 | cut -d: -f1) _LINE_TO_EDIT=$(($_BEST_PATH_LINE_NUMBER+2)) _TOTAL_LINES=$( wc -l text.log) if [[ $_LINE_TO_EDIT -gte $_TOTAL_LINES ]]; then # if the line we want to add this to is greater than the size of the file, append it sed -i .bak "a\$i#This is added automatically\n\n192.168.1.2 domain1. com" text.log else # else insert it directly sed -i .bak "$i\#This is added automatically\n\n192.168.1.2 domain1. com" text.log fi 

Simply enter in the ip address you’re trying to find and this script matches on the first occurrence of it.

Читайте также:  Linux mint режим совместимости

This inline sed should work:

sed -i.bak 's/^192\.168\.1\.2.*$/& ADDED/' hosts 
  1. This sed command find lines that start with 192.168.1.2
  2. If found it adds ADDED at the end of those lines

Shell — How to add lines to end of file on Linux, It depends on the last added line, not your current command. When you do the $ echo «foobar» >> file,the newline is already there. If you do $ echo -n «foobar» >> file, you won’t append the newline to the end of the line, so you’ll write in the same line. – user897079 Mar 24, 2015 at 21:29 11 Code sampleecho ‘VNCSERVERS=»1:root»‘ >> /etc/sysconfig/configfileecho ‘VNCSERVERARGS[1]=»-geometry 1600×1200″‘ >> /etc/sysconfig/configfileFeedback

Adding a line at the end of a bash file

-bash: /root/.bashrc: line 100: conditional binary operator expected -bash: /root/.bashrc: line 100: syntax error near `"$HOME/.rvm/scripts/rvm"' -bash: /root/.bashrc: line 100: `[[ -s "$HOME/.rvm/scripts/rvm"]] && source "$HOME/.rvm/scripts/rvm" #Load RVM into a shell session *as a function*' 

I have got those output after I added

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*` 

does anyone know how to fix this ?

I would like to load rvm into the shell session

A space is missing before ]] (guessing from the error message, not shown in you code, though).

Why would Vim add a new line at the end of a file?, You can put the following line into your .vimrc autocmd FileType php setlocal noeol binary Which should do the trick, but actually your approach is somewhat wrong.

How to add a column at the end of .txt file in Linux?

I want to add a new column containing only ‘2’ as a value in each row at the end of the a .txt file and save new file as output. I saved my input.txt file from R as a tab separated file. I have tried the following code;

awk -v RS=’\r\n’ ‘BEGIN ‘ input.txt > output.txt

but it adds 2 as an additional value to the last column and also adds a blank row after every data row in the output.txt file

SAMPLE rs1 rs2 rs3 rs4 rs5 rs6 rs7 rs8 rs9 rs10 rs11 6 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 10 1 1 14 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 34 0 0 0 0 0 0 0 0 0 0 0 40 1 1 1 41 0 0 0 0 0 0 0 0 0 0 0 46 1 58 1 67 0 0 0 0 0 0 0 0 0 0 69 1 71 0 0 0 0 0 0 0 0 0 0 0 76 0 0 0 0 0 0 0 0 0 0 0 78 0 0 0 0 0 0 0 0 0 0 0 80 1 83 0 0 0 0 0 0 0 0 0 0 84 1 86 1 90 1 92 0 0 0 0 0 0 0 0 0 0 94 0 0 0 0 0 0 0 0 0 0 95 0 0 0 0 0 0 0 0 0 0 

what I am getting as output.txt using above code;

SAMPLE rs1 rs2 rs3 rs4 rs5 rs6 rs7 rs8 rs9 rs10 rs112 6 0 0 0 0 0 0 0 0 0 0 02 7 0 0 0 0 0 0 0 0 0 0 02 10 1 1 2 14 0 0 0 0 0 0 0 0 0 0 02 19 0 0 0 0 0 0 0 0 0 0 02 34 0 0 0 0 0 0 0 0 0 0 02 40 1 1 12 41 0 0 0 0 0 0 0 0 0 0 02 46 1 2 58 1 2 67 0 0 0 0 0 0 0 0 0 02 69 1 2 71 0 0 0 0 0 0 0 0 0 0 02 76 0 0 0 0 0 0 0 0 0 0 02 78 0 0 0 0 0 0 0 0 0 0 02 80 1 2 83 0 0 0 0 0 0 0 0 0 02 84 1 2 86 1 2 90 1 2 92 0 0 0 0 0 0 0 0 0 02 94 0 0 0 0 0 0 0 0 0 02 95 0 0 0 0 0 0 0 0 0 02 

How do add a column of 2s at the end? Also, is there a way to add a header to (e.g., extra) this column in the output.txt file?

$ awk -v RS='\r\n' 'BEGIN ' input.txt > output.txt 

Источник

Читайте также:  Linux apache all modules

How to Add a Line to the End of a File in Linux

This Linux article describes different methods to append a line to the end of a file.

By reading this tutorial, you will be able to append new lines to the end of files both with and without superuser privileges in Linux using different techniques. All methods include examples.

The content is valid for all Linux distributions.

All instructions contain real screenshots to make it easy for any Linux user to understand and apply them.

Adding a line to the end of a file that doesn’t require superuser privileges

This section explains how to add a new line to the end of a file that does not require superuser privileges. After the following instructions, this tutorial shows how to add lines to files requiring privileges.

To begin, I created a file named linuxhintaddline. To see its content, I will use the less command as shown in the figure below.

As you can see below, the file contains 3 lines: “line 1”, “line 2”, and “line 3”.

The syntax to add a line to the end of the file is the following: “Line Content” is the text you want to add, and “File” is the file to which you want to add the line.

Therefore, if I want to add the “line 4” to the linuxhintaddline file, I execute the command shown in the following screenshot:

I want to check the file to confirm the line was properly appended.

As you can see in the image below, the fourth line was added successfully.

There are different ways to add lines. You also can use the printf command. The syntax is the following:

If I want to add a “line 5” last line to the linuxhintaddline file, I run the command shown below.

Now, just to use a different command, I will confirm if the line was added by executing the command below.

The line was successfully added.

The following example describes how to add multiple lines to the end of a file.

How to a line to the end of a file requiring privileges

As said previously, the method above won’t affect files without superuser privileges. This section shows how to do it.

As you can see below, when trying to use the former command, I get an error because of a lack of permissions.

The syntax to append lines to files with root or sudo privileges is the following:

For this example, I want to add the “line 5” text to the /root/linuxhintaddline file. To do it, I use the syntax explained above, as you can see in the figure below.

Again, I use the less command to confirm the line was successfully added.

As you can see below, the line was properly added.

Another way to read the new line and write it into a file is by combining the echo command with tee through a pipe. The syntax is the following:

Therefore, if I want to add the “line 6” line with privileges I type the command shown in the figure below.

Читайте также:  Linux отменить последнее удаление

To check if the line was properly added, I will run the less command again followed by the path.

And as you can see, the line was appended.

That’s how you can easily append lines to the end of files in Linux.

Conclusion:

Appending lines to the end of files is easy, and Linux flexibility allows more than one technique, even more than the one described in this document. As said previously, the above instructions are useful on all Linux distributions. It is recommended to write all commands by yourself rather than copying and pasting them.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

How do I insert a newline/linebreak after a line using sed

It took me a while to figure out how to do this, so posting in case anyone else is looking for the same.

6 Answers 6

For adding a newline after a pattern, you can also say:

G Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space. 

Incidentally, this happens to be covered in sed one liners:

 # insert a blank line below every line which matches "regex" sed '/regex/G' 
sed -i '' '/pid = run/ a\ \ ' file.txt 

Finds the line with: pid = run

file.txt before

; Note: the default prefix is /usr/local/var ; Default Value: none ;pid = run/php-fpm.pid ; Error log file 

and adds a linebreak after that line inside file.txt

file.txt after

; Note: the default prefix is /usr/local/var ; Default Value: none ;pid = run/php-fpm.pid ; Error log file 

Or if you want to add text and a linebreak:

sed -i '/pid = run/ a\ new line of text\ ' file.txt 

file.txt after

; Note: the default prefix is /usr/local/var ; Default Value: none ;pid = run/php-fpm.pid new line of text ; Error log file 

sed ‘a’ is a pretty good idea . I would’ve used sed -e ‘/pid = run/ s/$/\n/’ or sed -e ‘/pid = run/ s/$/\nnew line of text/’

A simple substitution works well:

$ printf "Hi\nBye\n" | sed 's/H.*$/&\nJohn/' Hi John Bye 

To be standard compliant, replace \n by backslash newline :

$ printf "Hi\nBye\n" | sed 's/H.*$/&\ > John/' Hi John Bye 

To answer the question as asked, you’d have to do sed ‘s/pattern.*/&\n/’ , otherwise you’ll insert the newline right after the match instead of at the end of the line. Also note that your solution requires GNU sed (won’t work on BSD/OSX, because the BSD sed implementation doesn’t support escape sequence \n in the replacement string).

++; there are many differences, unfortunately; BSD Sed implements only a few extensions to the POSIX spec., while GNU Sed implements many more — I’ve tried to compile the differences in this answer.

With GNU sed and ripgrep (rg) in a terminal this worked for me: rg . —color always -in -e ‘apples’ |sort | sed ‘s|\./|======================\n|’ to separate rg results lines, where the raw results begin with ./10643.html.

It will add a return after the pattern while g will replace the pattern with a blank line.

If a new line (blank) has to be added at end of the file use this:

Another possibility, e.g. if You don’t have an empty hold register, could be:

Explanation:
/pattern/ = apply sequence of commands, if line with pattern found,
p = print the current line,
; = separator between commands,
s/.*// = replace anything with nothing in the pattern register,
then automatically print the empty pattern register as additional line)

Источник

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