j. doe
j. doe

Reputation: 173

sed script delete line before if blank line

I have a file that looks like this:

name:height:weight

janice:120:209
mark:150:349
ray:90:204
elle:140:129

----------

I want to write a sed script that adds a line containing ~ at the beginning + end of the file and before and after every person entry, like this:

~
name:height:weight

~
janice:120:209
~
mark:150:349
~
ray:90:204
~
elle:140:129
~

----------
~

My current script and output look like this, but I dont know how I can add '~' between every person entry:

1i ~
$a ~
~
name:height:weight

janice:120:209
mark:150:349
ray:90:204
elle:140:129

----------
~

Upvotes: 3

Views: 74

Answers (4)

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed -zE 's/.*/~\n&/mg;s/~\n//2;s/(.*)~\n(.*~)/\1\2/' file

Puts ~\n above and below all non-empty lines, then removes the second and second from last occurrences of ~\n.

Alternative:

sed -e '1i~' -e '1b;N;/^\n$/!a~' -e 'P;D' file

Upvotes: 0

Panwen Wang
Panwen Wang

Reputation: 3855

Probably the shortest answer among all those:

Sed script:

i ~
/^name:height:weight$\|^$/N
$a ~

Explanation:

  • Line 1: insert ~ before each line
  • Line 2: if we hit the header or the empty line before -------- we skip the next ~
  • Line 3: append ~ to the end of the stream

Let's save it script.sed, and test it out:

> sed -f script.sed test.txt
~
name:height:weight

~
janice:120:209
~
mark:150:349
~
ray:90:204
~
elle:140:129
~

----------
~

Tested with

> sed --version
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

Upvotes: 0

William Pursell
William Pursell

Reputation: 212654

This doesn't exactly match your output, but it does match what I described in the comment and which you confirmed is what you want:

awk '/./ && !/^---/{print "~"}1; END{print "~"}' input

Tweak as needed depending on your actual requirements.

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 204638

This will work using any awk in any shell on every UNIX box:

$ cat tst.awk
BEGIN { RS="" }
NR == 1 { print "~\n" $0 "\n"; next }
/^-+$/  { print $0 "\n~"; next }
{
    gsub(/\n/,"\n~\n")
    print "~\n" $0 "\n~\n"
}

.

$ awk -f tst.awk file
~
name:height:weight

~
janice:120:209
~
mark:150:349
~
ray:90:204
~
elle:140:129
~

----------
~

Upvotes: 0

Related Questions