Add Blank Line Between Lines from Different Groups

Add blank line between lines from different groups

Awk could do it:

awk '{if(NR > 1 && $2 != prev_two){printf "\n";} prev_two=$2; print $0}' A

A being the file name.

How to add blank line between lines from different ip groups

This:

$ awk '{if(NR > 1 && $1 != prev_two){printf "\n";} prev_two=$1; print $0}' localhost.txt >> result.txt
$ cat result.txt
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18

192.168.1.20 date 6/2/2012
192.168.1.20 host name riko

192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84

should work. It's the first field you want to group by, not the second (hence the $1).

How to add blank lines between definitions?

You have

MaxEmptyLinesToKeep: "0"

which needs to be set to 1 instead, to ensure that multiple empty lines between definitions are not removed.

Importantly, the value needs to be an unsigned type, not a string.

So the correct setting for what you want should be

MaxEmptyLinesToKeep: 1

Note that this won't work if you don't already have at least one blank line between definitions, but from clang-format 14, you can use

SeparateDefinitionBlocks : Always

which will add an empty line between every definition. The other options are Leave which doesn't change anything, and Never, which will remove blank lines, if any.

I don't think there's an option that lets you do this in previous versions.

Source: Clang format style options.

Insert blank row between groups and maintain original order

The issue is that after the column 'month' got changed to an ordered factor, "" is not specified as one of the levels. So, naturally any value that is not a level is treated as missing value and hence we get the NA. Correction can be done at the earlier step by including the "" as one of the levels

df$month <- ordered(df$month, levels = c("2", "5", "8", "11", "annual", ""))

NOTE: The order of the "" is not clear. So, it is specified as the last level

How to add N blank lines between all rows of a text file?

You may use with GNU sed:

sed -i 'G;G;G' file

The G;G;G will append three empty lines below each non-final line.

Or, awk:

awk 'BEGIN{ORS="\n\n\n"};1'

See an online sed and awk demo.

If you need to set the number of newlines dynamically use

nl="
"
awk -v nl="$nl" 'BEGIN{for(c=0;c<3;c++) v=v""nl;ORS=v};1' file > newfile

sed or awk insert blank line between two known lines

The sed one-liner is:

sed -e '/td class="ris"/ { n; /^<\/td>.*$/ { s/^.*$/\n&/ }}'

To explain the solution, let's break it into more lines:

#!/bin/sed -f
/td class="ris"/ { # For every td of "ris" class,
n # read the next line.
/^<\/td>.*$/ { # If the next line starts with the closing td,
s/^.*$/\n&/ # insert one \n before it.
}
}

Note that we are in the regular sed mode, that means the pattern space is printed as soon as we read next line.
That makes the solution simple, no need to play with the hold space, sed will do it for us.



Related Topics



Leave a reply



Submit