Hi
i am reading a tutorial on sed
below command was given in tutorial. i am not able to understand the working of below command also this command is now working in my enviroment.
$ sed -n '1~2p' alarm
sed: 1: "1~2p": invalid command code ~
$
need your assitance here
Hello,
I think it should be as follows.
sed -n '1,2p' file_name
To print the first two lines.
Thanks,
R. Singh
~ is a GNU extension, so your need GNU sed for that. m~n means match every n 'th line starting with line m . Used like this, it means print every other line, starting with the first line. The same can be achieved in any sed like this:
sed -n 'p;n' file_name
The printing every other line thing is a bit of a tangle. When people give you this requirement, they may mean start with line 1, or possibly line 2. Printing odd-numbered lines vs. even-numbered lines.
Scrutinizer's code works either way. His example gives odd numbers, changing to
sed -n 'n;p' file_name
gives even numbers.
Hi
the suggested command still not working
$ sed -n 'p:n' alarm
sed: 1: "p:n": extra characters at the end of p command
$
scriptor
below snippet takem from online tutorial which i am using for learning sed
If we want to print every other line, we can specify the interval after the "~" character. The following line will print every other line starting with line 1:
sed -n '1~2p' BSD
can ypu please explain me this line also . what does it mean here
If we want to print every other line,
by saying every other line i am not getiing their point
That should be a ; not a : . sed -n 'p;n' , not sed -n 'p:n'
thx working now .
substitute command is not working
here i am replacing foo with bar .
sed 's/foo/bar' f1
Your substitute command is incomplete. Try:
sed 's/foo/bar/' f1