How to use sed to delete the next line in Solaris

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

Ref.

Location
New York <---- delete
USA

Location
London <---- delete
United Kingdom

I tried sed '/Location/{n; d}' sed '/Location/{n; d}' , which worked on Linux but did not work on tanning beds.

Thanks.

+4
source share
3 answers

As I mentioned in your answer about your other question, Solaris sed is an old school and requires more hands (or, to put it another way), is more fussy about the syntax.

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

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

More generally, everything that can be on a new line inside {...} needs a comma delimited delimiter when it is collapsed onto one line. However, you cannot collapse the "a", "i", "c" commands on one line, as you can on Linux.

In the standard Solaris saddle, the commands' a ', i', 'c' need a trailing '\' without NO spaces or tabs after it, as much data as you like (probably within some K) on \n completed lines ( NO \r s) followed by an empty string.

Newer Solaris installations can also be installed /usr/xpg4/bin/sed . Try

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

If you're lucky, it will support your shortcut syntax. I do not have access to any solar cars to test this.

Finally, if that doesn't work, there are GNU tool packages that can be installed that will have sed , which is much more like what you are used to with Linux. Ask your system administrators if GNU tools are already available or can be installed. I'm not sure which version of gnu sed started supporting the "relaxed" syntax, so don't assume that it will be fixed without testing :-)

Hope this helps.

PS Welcome to StackOverflow and let me remind you of the three things we usually do here: 1) As you get help, try giving it as well by answering questions in your area of ​​expertise. 2) Read the frequently asked questions, http://tinyurl.com/2vycnvr , 3) When you see good Q&A, vote for them using the gray triangles, http://i.imgur.com/kygEP.png , as trust in the system is based on the reputation that users gain by sharing their knowledge. Also, do not forget to accept the answer that better solves your problem, if any, by clicking the check mark, http://i.imgur.com/uqJeW.png

+6
source

You can add the following line to the current one and then delete everything that is not Location :

 $ cat text Location New York <---- delete USA Location London <---- delete UK $ sed '/Location/{N;s/Location.*$/Location/;}' text Location USA Location UK 

I don't have Solaris, so I would like to know if this works.

0
source

Did this AWK solution help you -

 [jaypal~/temp]$ cat a.txt Location New York <---- delete USA Location London <---- delete UK 

Updated to save empty string -

!NF used to save blank lines. This means that if the number of fields is 0, just print the line. NF is a built-in variable that tracks the number of fields in a record. If we come across an empty line, we skip the rest of the processing and move on to the next line.

!/Location/ will print lines. This is necessary to preserve lines that Location does not follow. Printing is an implicit action in AWK whenever a pattern is valid.

The third step / action is where we print the line when it matches RegEx /Location/ . Besides printing a line, we get a line twice, which effectively deletes the next line and then prints it.

 [jaypal~/temp]$ awk '!NF{print;next}; !/Location/; /Location/{print;getline;getline;print}' INPUT_FILE Location USA Location UK 
0
source

Source: https://habr.com/ru/post/1383027/


All Articles