Prefira linhas com prefixo de arquivo usando sed ou awk

0

Como faço para alterar um arquivo de entrada de amostra para obter algo como o arquivo de saída abaixo?

Arquivo de entrada:

/tmp/logs
10/13/15 00:19:13 templogs1
10/13/15 00:19:13 dummylogs
10/13/15 01:19:12  test
10/13/15 01:19:12 error
/tmp/error
10/13/15 00:16:10 x
10/13/15 00:16:10 y
10/13/15 01:16:10 z
10/13/15 01:16:10 a
/tmp/access
10/13/15 00:56:14 b
10/13/15 00:56:14 c

Arquivo de saída:

/tmp/logs 10/13/15 00:19:13 templogs1
/tmp/logs 10/13/15 00:19:13 dummylogs
/tmp/logs 10/13/15 01:19:12  test
/tmp/logs 10/13/15 01:19:12 error
/tmp/error 10/13/15 00:16:10 x
/tmp/error 10/13/15 00:16:10 y
/tmp/error 10/13/15 01:16:10 z
/tmp/error 10/13/15 01:16:10 a
/tmp/access 10/13/15 00:56:14 b
/tmp/access 10/13/15 00:56:14 c
    
por Chittha Shetty 16.10.2015 / 17:57

2 respostas

3

Tente com sed

sed '
    /^\//{                    # execite block if line start with «/»
        h                     # put line into hold-space
        d                     # clean, return to beginning
        }                     # end block
    G                         # append hold-space to line
    s/\(.*\)\n\(.*\)/ /   # exchange first and last part of line
    ' input.file > output.file

e outra variante

sed '
    /^\//{                    # execute from line started with «/»
        :1                    # mark return point
        N                     # get next line
        /\n\//D               # if such next line started «/» remove previous
                              #+and starts from the beginning with last line
        s/\n/ /p              # change newline with space and prints both lines
        s/ .*//               # removes last line
        $!b1                  # return to marked point with fist line if not end
        d                     # clean all (finish)
        }
    ' input.file > output.file
    
por 16.10.2015 / 18:55
0

tente

awk 'substr($1,1,1) == "/"  { prefix=$0 ; next ; }
   { printf "%s %s\n",prefix,$0;}' 

isto irá capturar o prefixo quando eles começarem com ' / ' ...

    
por 16.10.2015 / 18:18

Tags