Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sed to delete next line on Solaris

Tags:

sed

solaris

I trying to use sed in finding a matching pattern in a file then deleting the next line only.

Ex.

Location
New York <---- delete
USA

Location
London <---- delete
UK

I tried sed '/Location/{n; d}' that work on linux but didn't work on solaris.

Thanks.

like image 501
Apiwat Manasmascharoen Avatar asked Nov 25 '11 10:11

Apiwat Manasmascharoen


People also ask

How do you delete a line in a file with sed?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

Which option from sed command is used to delete 2nd line in a file?

The below sed command removes the second line in a file. Use the negation (!) operator with d option in sed command. The following sed command removes all the lines except the header line.

Can we delete content in a file by using sed command?

There is no available to delete all contents of the file. How to delete all contents of the file using sed command.


1 Answers

As I mentioned in my answer about your other sed question, Solaris sed is old-school AND needs more hand-holding (or to put it another way), is more fussy about it's syntax.

All you need is an additional ';' char placed after the `d' char, i.e.

sed '/Location/{n; d;}'

More generally, anything that can be on a new-line inside {...} needs a semi-colon separator when it is rolled up onto a single line. However, you can't roll up the 'a', 'i', 'c' commands onto a single line as you can in Linux.

In Solaris standard sed, the 'a', i', 'c' commands need a trailing '\' with NO spaces or tabs after it, as much data as you like (probably within some K limit) on \n terminated lines (NO \r s), followed by a blank line.

Newer installations of Solaris may also have /usr/xpg4/bin/sed installed. Try

/usr/xpg4/bin/sed '/Location/{n; d}' 

If you're lucky, it will support your shortcut syntax. I don't have access to any solaris machines anymore to test this.

Finally, if that doesn't work, there are packages of GNU tools that can be installed that would have a sed that is much more like what you're used to from Linux. Ask your sys-admins if GNU tools are already there, or if they can be installed. I'm not sure what version of gnu sed started to support 'relaxed' syntax, so don't assume that it will be fixed without testing :-)

I hope this helps.

P.S. Welcome to StackOverflow and let me remind you of three things we usually do here: 1) As you receive help, try to give it too, answering questions in your area of expertise 2) Read the FAQs, http://tinyurl.com/2vycnvr , 3) When you see good Q&A, vote them up by using the gray triangles, http://i.imgur.com/kygEP.png , as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign , http://i.imgur.com/uqJeW.png

like image 134
shellter Avatar answered Sep 28 '22 05:09

shellter