Piste: • generer_un_certificat_signe_par_sa_propre_autorite • ouvrir_des_fichiers_directement_d_un_ftp_avec_gedit • 10_balises_html_rarement_utilisees_qui_valent_le_coup • installer_le_module_apc_sur_une_debian_etch_avec_php_5.2 • shell_intercaler_une_chaine_de_caracteres_dans_une_liste
Shell - Intercaler une chaîne de caractères dans une liste
Imaginons le fichier suivant nommé “file.txt” :
LIGNE A LIGNE B LIGNE C
Pour obtenir la sortie suivante :
LIGNE A * NOUVELLE CHAINE * LIGNE B * NOUVELLE CHAINE * LIGNE C
Il est possible d'utiliser l'une des deux commandes suivantes :
cat file.txt | perl -pe 'print "* NOUVELLE CHAINE *\n" if( $. != 1 );'
ou
cat file.txt | awk '{if (NR == 1) { tmp=$0 } else { print tmp"\n* NOUVELLE CHAINE*"; tmp=$0 }} END { print tmp }'
L’intérêt est de ne pas avoir la chaîne “* NOUVELLE CHAINE *” en début ou fin de sortie.