Borrar las dos primeras líneas de un archivo usando BASH o awk o sed o lo que sea


Estoy tratando de eliminar las dos primeras líneas de un archivo simplemente no imprimiéndolo en otro archivo. No estoy buscando algo elegante. Aquí está mi (fallido) intento de awk:

awk '{ (NR > 2) {print} }' myfile

Que arroja el siguiente error:

awk: { NR > 2 {print} }
awk:          ^ syntax error

Ejemplo:

Contenido de 'myfile':

blah
blahsdfsj
1 
2
3
4

Lo que quiero que sea el resultado:

1
2
3
4
Author: Sam, 2012-01-14

4 answers

Use cola:

tail -n+3 file

De la página de manual:

   -n, --lines=K
          output the last K lines, instead of the last 10; or use  -n  +K
          to output lines starting with the Kth
 59
Author: RobS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-13 21:58:02

Qué tal:

tail +3 file

O

awk 'NR>2' file

O

sed '1,2d' file
 23
Author: anubhava,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-13 21:55:27

Ya casi estás ahí. Prueba esto en su lugar:

awk 'NR > 2 { print }' myfile

Awk está basado en reglas, y la regla aparece desnuda (es decir, sin llaves) antes del bloque que ejecutaría si pasa.

También como Jaypal ha señalado, en awk si todo lo que desea hacer es imprimir la línea que coincide con las reglas, incluso puede omitir la acción, simplificando así el comando a:

awk 'NR > 2' myfile
 20
Author: Chris J,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-13 22:44:48

awk se basa en declaraciones pattern{action}. En su caso, el pattern es NR>2 y el action desea realizar es print. Este action es también el default action de awk.

Así que aunque {[14]]}

awk 'NR>2{print}' filename

Funcionaría bien, puedes acortarlo a {[14]]}

awk 'NR>2' filename.

 4
Author: jaypal singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-13 22:42:55