Ayodeji Babaniyi
Ayodeji Babaniyi

Reputation: 41

How do you add a space on top of a comment in a bash script

Given a script like this

# I love this comment
echo "Code code code"
# I love this comment more
echo "More code, more code"

I want to automatically change it to

# I love this comment
echo "Code code code"

# I love this comment more
echo "More code, more code"

How do I accomplish that

Upvotes: 2

Views: 97

Answers (3)

potong
potong

Reputation: 58420

This might work for you (GNU sed):

sed '1b;/^#/i\\' file

Upvotes: 0

Jotne
Jotne

Reputation: 41456

Here is an awk

awk '/^[[:blank:]]*#/ && NR>1 {$0=RS$0}1' file
# I love this comment
echo "Code code code"

# I love this comment more
echo "More code, more code"

It adds a blank line before every line starting with # except the first line.

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 157992

You can use this:

sed '2,$ s/^#/\n#/' 

It will add a new line in front of every # on the beginning of a line, except of the first line.

Upvotes: 5

Related Questions