How do I delete a line only if it is at a specified line number and it matches the pattern?
For example:
- I want to delete (
d
); - the third line (
3
); - if it's blank (
^$
);
The following syntax:
cat file | sed '3 /^$/d'
Returns the following error:
sed: -e expression #1, char 3: unknown command: `/'
cat
in your example? Any reason to complicate things and not runsed '3 /^$/d' file
? – Anthon Oct 03 '16 at 06:35sed -i '4s/foo/bar/g' file
(substitute only on one line), but that only works sinces///
is an operation, and a no-op if it doesn't match. (So yah, voted for reopen) – ilkkachu Oct 03 '16 at 12:13