Which command would you use?

You’re processing a file ‘allmostempty.txt’ with lots of empty lines. You want to get rid of these empty lines as they are totally useless for your purpose. Which command would you use? (choose the best answer)

You’re processing a file ‘allmostempty.txt’ with lots of empty lines. You want to get rid of these empty lines as they are totally useless for your purpose. Which command would you use? (choose the best answer)

A.
grep -v ‘^$’ < allmostempty.txt

B.
sed ‘s/^$//’ < allmostempty.txt

C.
grep ‘.+’ < allmostempty.txt

D.
sed -e ‘.+’ < allmostempty.txt

Explanation:
-e PATTERN, –regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning with -.

-v, –invert-match
Invert the sense of matching, to select non-matching lines.

The caret ^ and the dollar sign $ are metacharacters that respectively match the empty string at the beginning and end of a line.
The symbols <</B> and > respectively match the empty string at the beginning and end of a word. The symbol b matches the empty string at the edge of a word, and B matches the empty string provided it’s not at the edge of a word.

A regular expression may be followed by one of several repetition operators:

?
The preceding item is optional and matched at most once.
*
The preceding item will be matched zero or more times.
+
The preceding item will be matched one or more times.
{n}
The preceding item is matched exactly n times.
{n,}
The preceding item is matched n or more times.
{n,m}
The preceding item is matched at least n times, but not more than m times.



Leave a Reply 0

Your email address will not be published. Required fields are marked *