0

I have the configuration file, i want matching particular line like "server.port=5480". Leave that matching first remove rest matching line. Example

server.port=5480
server.port=5480
server.port=5480
ssl.engine = "enable"
ssl.use-sslv2 = "disable"
ssl.use-sslv3 = "disable"
ssl.cipher-list = "FIPS':' +3DES':'!aNULL"
ssl.engine = "enable"
ssl.use-sslv2 = "disable"
server.port=5480
server.port=5480
server.port=5480
server.port=5480

Desired output is only one line

server.port=5480
ssl.engine = "enable"
ssl.use-sslv2 = "disable"
ssl.use-sslv3 = "disable"
ssl.cipher-list = "FIPS':' +3DES':'!aNULL"
ssl.engine = "enable"
ssl.use-sslv2 = "disable"

Using sed I want this and one more this is not sequence line may be it comes variant

SSN
  • 846
  • 2
  • 7
  • 20
  • If you are open to a non `sed` solution, you can try piping your output to `sort -u`. – maxime.bochon Aug 25 '16 at 12:58
  • can you give me an example – SSN Aug 25 '16 at 13:09
  • Sort -u isn't exactly what OP wants since that would coalesce the ENTIRE file and leave just the unique entries. He just wants to remove the cases where there are multiple occurrences one after the other. – Chem-man17 Aug 25 '16 at 13:12
  • @SatheeshSivaNallathambi It can be used either directly: `sort -u your_file`, or after a pipe: `cat your_file | sort -u`. But as explained by VarunMandalaparthy, all duplicates will be removed, not only the one you want, and the whole file will be sorted. – maxime.bochon Aug 25 '16 at 14:42

1 Answers1

5

Let's use awk and print all those lines that either do not contain server.port or it appears for the first time:

$ awk '!(/server.port/ && c++)' file
server.port=5480
ssl.engine = "enable"
ssl.use-sslv2 = "disable"
ssl.use-sslv3 = "disable"
ssl.cipher-list = "FIPS':' +3DES':'!aNULL"
ssl.engine = "enable"
ssl.use-sslv2 = "disable"

Note I use this approach because I see other lines occurring more than once.

How does this work?

A true condition in awk triggers printing the whole line. So if you say awk '1' file, all the file will be printed.

What we are doing here is the contrary: limit printing on certain cases. And these cases are when server.port occurs for the 2nd, 3rd... time.
Since /server.port/ matches the lines that have "server.port" in it, we then also check if the variable c is positive or not.

  • When it is set for the first time we have !(True && 0++) which is !(True && False)!(False)True.

  • In the rest of cases, it is !(True && positive++) which is !(True && True)!(True)False, so no print is triggered.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Ya it's work fine, I want write on same file because i am using this file for other system – SSN Aug 25 '16 at 13:13
  • @SatheeshSivaNallathambi For inplace modifications check [awk save modifications inplace](http://stackoverflow.com/a/16529730/1983854) – fedorqui Aug 25 '16 at 13:17
  • thanks for your code i am running this code for several machine so i can't check is there gawk available there. co is there any other way and i am new in awk can u explain this code especially c++ purpose – SSN Aug 26 '16 at 05:02
  • @SatheeshSivaNallathambi in any awk flavour you can say `awk '...' file > tmp_file && tmp_file file`. This will always work. Regarding the code itself, see my update. – fedorqui Aug 26 '16 at 06:19
  • I saw `"gawk -i inplace '{print $1}' file"` this line it's good like `sed -i` . Thank for your code and explanation also now i understand – SSN Aug 26 '16 at 07:26