0

I can comment out certain lines through vi in files. It is strenuous activity to do it in the 100's of files. Can I put a condition through sed or awk, for example, I want to put a condition as to comment out all the lines which have this client-ca-gh.ef.cd.1 and onwards.

The file has close to 500 lines, including 100 lines which include client1-100, which increases incrementally from 1 to 100. it is the same in all files, the only difference is that due to some spacing in the files, the line numbers do not match. For example, in one file client5 is on line 3 and in another one, it might be on 4. otherwise I would have used sed for all the files with line range

:3,5s/client/#client
1 client-ca-gh.ef.cd.1
2 client-ca-gh.ef.cd.2
3 #client-ca-gh.ef.cd.3
4 #client-ca-gh.ef.cd.4
5 #client-ca-gh.ef.cd.5
muru
  • 72,889
  • 1
    Please reformat your posting. It looks like you're using bolding and double-spacing and an image instead of code format, but I'm not certain so I don't want to try to correct it and maybe get it wrong. Here is a summary of how to use code format. – Gordon Davisson Feb 01 '21 at 20:13
  • 3
    Is the file sorted and happens to be starting at line 1 = site-client 1 with client numbers continuously (=no missing integers) increasing? – FelixJN Feb 01 '21 at 20:23
  • yes it starts with the client1 and ends with client100 – Muhammad Ilyas Feb 01 '21 at 20:39
  • @MuhammadIlyas so the file has exactly 100 lines? On that is based my answer. – schrodingerscatcuriosity Feb 01 '21 at 20:41
  • @MuhammadIlyas Don't add line numbers unless they are in the file. There are other lines with other text, or it's just one file with similar pattern local-ip 0.0.0.0 site-client<number>? – schrodingerscatcuriosity Feb 01 '21 at 20:50
  • The file has close to 500 lines, including 100 lines which include client1-100, which increases incrementally from 1 to 100. it is the same in all files, the only difference is that due to some spacing in the files, the line numbers do not match. For example, in one file client5 is on line 3 and in another one, it might be on 4. otherwise I would have used sed for all the files with line range. – Muhammad Ilyas Feb 01 '21 at 20:52
  • @MuhammadIlyas This is crucial info to find a proper solution. Please [edit] the question adding it. – schrodingerscatcuriosity Feb 01 '21 at 20:54
  • @schrodigerscatcuriosity I have edited the question. But the solution you gave me is commenting out all the lines, matching the pattern, in the script. But when I tested your solution as given, that is working fine. not sure why it does not work for my files. – Muhammad Ilyas Feb 02 '21 at 00:12

2 Answers2

1

If I understand you correctly, you can do this with sed, assuming the pattern client<number>$ is only present on those lines. Using other sample content:

local-ip 0.0.0.0 site-client1
foo
local-ip 0.0.0.0 site-client2
local-ip 0.0.0.0 site-client3
local-ip 0.0.0.0 site-client4
local-ip 0.0.0.0 site-client5
foo

local-ip 0.0.0.0 site-client6 local-ip 0.0.0.0 site-client7 local-ip 0.0.0.0 site-client8 local-ip 0.0.0.0 site-client9 ...

$ sed -e '/client[0-9]*$/s/^/#/' -e 's/^#\(.*client[1-2]\)$/\1/' file
local-ip 0.0.0.0 site-client1
foo
local-ip 0.0.0.0 site-client2
#local-ip 0.0.0.0 site-client3
#local-ip 0.0.0.0 site-client4
#local-ip 0.0.0.0 site-client5
foo

#local-ip 0.0.0.0 site-client6
#local-ip 0.0.0.0 site-client7
#local-ip 0.0.0.0 site-client8
#local-ip 0.0.0.0 site-client9

1

A more general solution using awk

For a file like:

header
header
client-ca-gh.ef.cd.1
client-ca-gh.ef.cd.4
client-ca-gh.ef.cd.2
client-ca-gh.ef.cd.3
other stuff

client-ca-gh.ef.cd.5 more stuff

we can a) search for lines with client-ca-gh.ef.cd. and then b) if the last number is higher than a given value, we comment out the line.

awk -F. '/^client-ca-gh\.ef\.cd\./ { if ($NF >= 3) {$0="#"$0}} {print}' file

Result:

header
header
client-ca-gh.ef.cd.1
#client-ca-gh.ef.cd.4
client-ca-gh.ef.cd.2
#client-ca-gh.ef.cd.3
other stuff

#client-ca-gh.ef.cd.5 more stuff

It will make you more flexible regarding missing lines, number of header and other lines, spacings between the client lines or shuffled sorting.

For inplace editing, use sponge from moreutils or if you are running GNU awk (gawk) the inplace option:

 awk -F. 'AWK CODE' file | sponge file
 awk -i inplace -F. 'AWK CODE' file
FelixJN
  • 13,566