LinuxForum.Fans
Тема

Работа с текстом.

Автор: semiono 7 сообщений 4126 просмотров
#303524

Как пронумеровать строки в файле?
grep, sed, regex ?
Только по-проще покажите, потому что нужно для GnuWin32 под виндоус :)
sed, например, там без 'кавычек' работает: sed -i s/string1/string2/g file
Впрочем под linux тоже интересно.

#303525

nl входит в coreutils, так что быть должен...
для grep'а ключ -n смотри.
для sed'а через = можно, но не очень красиво получается...

#303537

Ответ есть в man cat.

Показать
text
$ echo -e "line\nline\nline" | cat -n
     1    line
     2    line
     3    line

#303591

Не совсем сформулировал задачу, нужно в готовый текст в начало строк нумерация, но наверное это сложнее

#303595
"semiono" написал:
Не совсем сформулировал задачу, нужно в готовый текст в начало строк нумерация, но наверное это сложнее

нет, не сложно, вот на примере готового файла hosts.allow

Показать
root@monitoring:/etc# cat hosts.allow
# /etc/hosts.allow: list of hosts that are allowed to access the system.
# See the manual pages hosts_access(5) and hosts_options(5).
#
# Example: ALL: LOCAL @some_netgroup
# ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
#
# If you're going to protect the portmapper use the name "portmap" for the
# daemon name. Remember that you can only use the keyword "ALL" and IP
# addresses (NOT host or domain names) for the portmapper, as well as for
# rpc.mountd (the NFS mount daemon). See portmap(8) and rpc.mountd(8)
# for further information.

теперь номеруем строчки

root@monitoring:/etc# cat hosts.allow | cat -n
1 # /etc/hosts.allow: list of hosts that are allowed to access the system.
2 # See the manual pages hosts_access(5) and hosts_options(5).
3 #
4 # Example: ALL: LOCAL @some_netgroup
5 # ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
6 #
7 # If you're going to protect the portmapper use the name "portmap" for the
8 # daemon name. Remember that you can only use the keyword "ALL" and IP
9 # addresses (NOT host or domain names) for the portmapper, as well as for
10 # rpc.mountd (the NFS mount daemon). See portmap(8) and rpc.mountd(8)
11 # for further information.

как видно, все пронумеровалось. но есть и второй способ. открой любой vi-подобный редактор. Например vim и напиши : set number. результат тот же самый увидешь. пример

1 # /etc/hosts.allow: list of hosts that are allowed to access the system.
2 # See the manual pages hosts_access(5) and hosts_options(5).
3 #
4 # Example: ALL: LOCAL @some_netgroup
5 # ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
6 #
7 # If you're going to protect the portmapper use the name "portmap" for the
8 # daemon name. Remember that you can only use the keyword "ALL" and IP
9 # addresses (NOT host or domain names) for the portmapper, as well as for
10 # rpc.mountd (the NFS mount daemon). See portmap(8) and rpc.mountd(8)
11 # for further information.
12 #
13
~
~
~
~
~
:set number

#303601
text

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys

if len(sys.argv) != 2: sys.stderr.write("Usage: %s <file>\n" % sys.argv[0]) raise SystemExit(1) fname = sys.argv[1] with open(fname, 'r') as f: lines = f.readlines() n = 0 with open(fname, 'w') as f: for line in lines: n += 1 f.write(str(n) + '\t' + line)

Правда, с форматированием лучше бы.
Можно собрать бинарник хоть под Linux, хоть под Windows:)

Ну, или man sed.

#303770
text
cat -n file.txt > new.txt

Да, у меня Ubuntu.
Нет, не глючит.

:D

Войдите или зарегистрируйтесь, чтобы ответить.