Это дает мне что-то очень похожее на набор для рисования линий vt100:
$ pstree -g 2 -U|less
Более общее решение с использованиемawk
Для файла типа:
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
мы можем a )искать строки с client-ca-gh.ef.cd.
и затем b )если последнее число больше заданного значения, мы закомментируем строку.
awk -F. '/^client-ca-gh\.ef\.cd\./ { if ($NF >= 3) {$0="#"$0}} {print}' file
Результат:
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
Это сделает вас более гибкими в отношении отсутствующих строк, количества заголовков и других строк, интервалов между клиентскими строками или сортировки в случайном порядке.
Для редактирования на месте используйте sponge
из moreutils
или, если вы используете GNU awk (gawk
), параметр на месте:
awk -F. 'AWK CODE' file | sponge file
awk -i inplace -F. 'AWK CODE' file
Если я вас правильно понял, вы можете сделать это с помощью sed
, предполагая, что шаблон client<number>$
присутствует только в этих строках. Использование другого демонстрационного содержимого:
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
Вы можете запустить этот скрипт Python как <name.py> -f <filename> -t <threshold>
для выполнения задания
import argparse
def main(filename, t):
file1 = open(filename, 'r') # Open and read the file
Lines = file1.readlines() # Get each line and store in variable
file1.close # Close the file
file1 = open(filename, 'w') # Open the file with Write persmissions
for line in Lines:
num = line.strip().split(".")[-1] # Get the number by splitting un char "."
try:
if int(num) >= t: # If the number is larger than the treshold
line = f"#{line}" #Replace the line with a "#" in front
except ValueError:
pass
file1.writelines(line) #Write the line on the file
return
## Pass parameters from command line to script
parser = argparse.ArgumentParser(description='Filename.')
parser.add_argument('-f', '--file')
parser.add_argument('-t', '--threshold')
args = parser.parse_args()
filename = args.file
t = args.threshold
main(filename, t)