The Dark Knight
The Dark Knight

Reputation: 5585

What does the regex '/^$/d' mean?

I was trying to remove blank lines in a file using bash script. Now when i was searching in the INTERNET, i came across two variations of it. In one, we can directly modify the source file and in the other we can store the out put in another file . Here are the code snippets :

sed -i '/^$/d' fileName.txt

sed '/^$/d' fileName.txt > newFileName.txt

What i could not understand is how the regex '/^$/d' can be interpreted as blank lines. I am afraid i am not good in regex . Can some one explain me this one ?

Also is there some other way to do it ?

Upvotes: 6

Views: 11490

Answers (6)

fardjad
fardjad

Reputation: 20404

Let's start with the regex explanation:

/^$/d

^ matches the beginning of the line and $ matches end of the line. so ^$ will match empty lines.

You're also using d flag with sed. This will remove the matched lines.

and -i switch in sed -i '/^$/d' fileName.txt makes sed remove the lines in-place. If you omit that, it will output the result to standard-output.

Upvotes: 4

Explosion Pills
Explosion Pills

Reputation: 191749

^$ represents an empty line because ^ is a zero-width anchor meaning the start of the line and $ is a zero-width anchor meaning the end of the line. Thus ^$ must be zero width (i.e. have no characters at all) to match. There also cannot be any characters before ^ on the line or after $ on the line.

Upvotes: 0

nneonneo
nneonneo

Reputation: 179462

/^$/: select lines that are empty (^ matches the start of line, $ matches the end of line, and so this matches lines that start and immediately end with no intervening content).
d: delete matched line.

Upvotes: 1

Marc B
Marc B

Reputation: 360732

/^$/d


/ - start of regex
^ - start of line
$ - end of line
/ - end of regex
d - delete lines which match

So basically find any line which is empty (start and ending points are the same, e.g. no chars), and delete them.

Upvotes: 15

Kent
Kent

Reputation: 195139

^ - start of a line
$ - end of line

so

/^$/ 

it matches lines with line beginning followed immediately by end of line. which means, empty lines.

the sed command with d means delete matched lines, that is, remove empty lines.

so basically:

sed '/regex/d(elete)'  --this is not a real command line, just for explanation.

Upvotes: 0

sigpwned
sigpwned

Reputation: 7443

/^$/d is a sed command that removes empty lines. It's actually two things stuck together: a regular expression /^$/ and a sed instruction d.

The /^$/ component is a regular expression that matches the empty string. More specifically, it looks for the beginning of a line (^) followed directly by the end of a line ($), which is to say an empty line. If there's anything in the line -- whitespace or otherwise -- that pattern won't match since the end of the line won't directly follow the beginning of the line.

The d component is a sed instruction that means "delete". In this usage, the d applies to any line that matches the given regular expression (/^$/), so it will delete any empty line.

Because sed is running in autoprint mode (without the -n switch), it will print all lines that aren't deleted -- so, in this case don't match /$^/ -- so that command ends up being a filter that removes all empty lines from the input.

Upvotes: 1

Related Questions