With sed or awk, move line matching pattern to bottom of file

Related searches

I have a similar problem. I need to move a line in /etc/sudoers to the end of the file.

The line I am wanting to move:

#includedir /etc/sudoers.d

I have tried with a variable

#creates variable value
templine=$(cat /etc/sudoers | grep "#includedir /etc/sudoers.d")

#delete value
sed '/"${templine}"/d' /etc/sudoers

#write value to the bottom of the file
cat ${templine} >> /etc/sudoers

Not getting any errors nor the result I am looking for.

Any suggestions?

With awk:

awk '$0=="#includedir /etc/sudoers.d"{lastline=$0;next}{print $0}END{print lastline}' /etc/sudoers

That says:

  1. If the line $0 is "#includedir /etc/sudoers.d" then set the variable lastline to this line's value $0 and skip to the next line next.
  2. If you are still here, print the line {print $0}
  3. Once every line in file is processed, print whatever is in the lastline variable.

Example:

$ cat test.txt
hi
this
is
#includedir /etc/sudoers.d
a
test
$ awk '$0=="#includedir /etc/sudoers.d"{lastline=$0;next}{print $0}END{print lastline}' test.txt
hi
this
is
a
test
#includedir /etc/sudoers.d

Need some sed magic: Moving marked lines to the beginning of a file, Move the matching lines to the end of the file: ed -s file <<< $'g/^\*/m$\nwq\n'. The g lobal subcommand marks every line that matches the pattern. Then, for each� $ awk '/Rent/{print}' file Rent,900 3. Since awk prints the line by default on a true condition, print statement can also be left off. $ awk '/Rent/' file Rent,900 In this example, whenever the line contains Rent, the condition becomes true and the line gets printed. 4. In the above examples, the pattern matching is done on the entire line

You could do the whole thing with sed:

sed -e '/#includedir .etc.sudoers.d/ { h; $p; d; }' -e '$G' /etc/sudoers

Move a line to end of file - UNIX and Linux Forums, Read a file /etc/inittab find the string starting with rcml and move it entirely towards the end Printing string from last field of the nth line of file to start (or end) of each line (awk I think) sed: how to move matched pattern to end of previous line. !/^desc:/ match lines that don't start with desc: not including the first line in the file NR>1. {print OFS} print an output field separator before the matched line. In awk the default OFS is . {printf "%s ",$0} print every line without a trailing newline. END{print OFS} after the file has been read add a trailing newline.

This might work for you (GNU sed):

sed -n '/regexp/H;//!p;$x;$s/.//p' file

This removes line(s) containing a specified regexp and appends them to the end of the file.

To only move the first line that matches the regexp, use:

sed -n '/regexp/{h;$p;$b;:a;n;p;$!ba;x};p' file

This uses a loop to read/print the remainder of the file and then append the matched line.

Use sed to move last line to top - UNIX and Linux Forums, I would also like to move the last line in the output file to the top. Can I use sed for By awk: Code: awk '{a[NR]=$0} END {print a[NR]; for (i=1;i<NR;i++) print a[i]}' urfile sed: how to move matched pattern to end of previous line. Hello, I'm new � 2. Replace all instances of a text in a particular line of a file using ‘g’ option ‘g’ option is used in `sed` command to replace all occurrences of matching pattern. Create a text file named python.txt with the following content to know the use of ‘g’ option. This file contains the word.

If you have multiple entries which you want to move to the end of the file, you can do the following:

awk '/regex/{a[++c]=$0;next}1;END{for(i=1;i<=c;++i) print a[i]}' file

or

sed -n '/regex/!{p;ba};H;:a;${x;s/.//;p}' file

[PDF] grep, awk and sed – three VERY useful command-line utilities Matt , regexp and instead you will get all the lines which have anything as an end of line, ie all For each line of the input file, it sees if there are any pattern-matching where the 'for' loop uses a 'getline' command to move through the file, and only � SED is a stream editor that performs basic text filtering and transformations on an input stream (a file or input from a pipeline). Delete Lines Matching a Specific Pattern in a File using SED. In our previous guide, we covered how to delete lines matching specific patterns in VIM. You can check by following the link below;

grep - Move pattern to beginning of line, Here we have used the sed 's group substitution method to get the desired output . The original file will be backed up as file.txt.bak , if you don't want that use to the end of the line (that's what the $ means), we know we're matching the right part in the first one. You could also do the same thing in awk . If you know a pattern on each of the two lines you want to swap, but not the full contents of the lines, you can do something like this: sed -n ' # turn off default printing /dog/{ # if the line matches "dog" h # put it in hold space :a # label "a" - the top of a loop n # fetch the next line /something/{ # if it matches "something" p # print it x # swap hold and pattern space bb # branch out

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern 'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match.

sed (or awk): add a new line “commit;” after every n-th line but only if next line starts with pattern Hot Network Questions Does a thread on Diamond Cup Grinding Wheel need a locknut?

Comments
  • What if #includedir /etc/sudoers.d doesn't exist in the file?
  • I am testing with a copy of /etc/sudoers and getting a result like a command has not terminated. awk '$0=="#includedir /etc/sudoers.d"{lastline=$0;next}{print $0}END{print lastline}` /etc/sudoers >
  • My apologies. I have updated the answer. I put a backtick in place of the single quote. That's been corrected.
  • This leaves a blank line where the #includedir was; to avoid that, use {h;d;} instead of x.
  • @BenjaminW.: good point, and include $p to also work when the pattern is on the last line
  • This may reduce the file if the regexp matches more than once and only move the latest match.
  • @potong: true, but given the pattern that is a desirable trait here