sed a \n

Alexandr Malusek malusek na hroch.ujf.cas.cz
Pátek Prosinec 22 09:47:59 CET 2000


djanko na infoware.cz (David Janko) writes:

> V mem manu tedy tohle neni :((.

Dokumentace ke GNU sed-u je bohuzel nekompletni. Dokumentace ke
komercnim Unixum je dostupna na Internetu, napr. http://docs.sun.com/,
http://techpubs.sgi.com/library/tpl/cgi-bin/init.cgi, ...

Princip cinnosti sed-u je tam popsan takto:

In normal operation, sed cyclically copies a line of input (less its
terminating NEWLINE character) into a pattern space (unless there is
something left after a D command), applies in sequence all commands
whose addresses select that pattern space, and copies the resulting
pattern space to the standard output (except under -n) and deletes the
pattern space. Whenever the pattern space is written to standard
output or a named file, sed will immediately follow it with a NEWLINE
character.

> Smim se zeptat jaky byste pouzil program (myslim jednoduchy streamovy,
> analogicky k sedu ...) ?

To zalezi na okolnostech. Pokud vim, tak sada klasickych Unixovych
nastroju takovy program neobsahuje (bohuzel). Na Internetu se urcite
nejake najdou - je to otazka i pro ostatni, nejsem ded vseved.

Perl by mel byt schopen s binarnimi soubory pracovat, takze misto sed
-e 's/A/B/g' lze pouzit perl -p -e 's/A/B/g'.

Dale je mozne binarni soubor napred prevest do ASCII, prohnat sed-em,
a pak prevest zpet do binarni formy. Napr. byte 0xOA na adrese 32
(dec) lze zamenit za byte 0x65 takto:

bin2hex < file | sed -e '32s/0A/65/1' | hex2bin > new_file

Programy bin2hex a hex2bin si kazdy muze upravit podle sebe,
jednoduche varianty pro uplnost prikladam.

$ cat bin2hex.c
#include <stdio.h>
int main()
{
  int c;
  while ((c = getchar()) != EOF)
    printf("%02X\n", c);
  return 0;
}

$ cat hex2bin.c
#include <stdio.h>
int main()
{
  int c;
  while (scanf("%2X", &c) == 1)
    putchar(c);
  return 0;
} 

--
A. Malusek  (malusek na ujf.cas.cz)
UJF AV CR


Další informace o konferenci Linux