bash: righe vuote, "invio a capo" e sostituzione live


Come rimuovere righe vuote ed invii a capo con bash? Bastano pochi comandi. Bash rulez! ;-)
 
prendo come riferimento il seguente file.txt che è composte da righe piene e vuote di questo tipo:
13227X

4004

4994XXX
13227X

4004XX

4994X
13227xxXXX

4004

499413227xXX
posso rimuovere le righe vuote con questo comando:
sed -e '/^$/d' file.txt
e ottengo:
XX13227
4004
x4994
13227
xXX4004
4994X
X13227
X4004
499413227xXX
4004
ma posso anche rimuovere tutte le righe vuote e quelle che cominciano con "X"
sed -e '/^$/d' file.txt  |sed -e '/^X/d'
e ottengo:
4004
x4994
13227
xXX4004
4994X
499413227xXX
4004
oppure posso rimuovere tutte le righe vuote e quelle che finiscono con "X"
 sed -e '/^$/d' file.txt  |sed -e '/X$/d'
e ottengo:
XX13227
4004
x4994
13227
xXX4004
X13227
X4004
4004
se invece voglio posso rimuovere tutte le righe vuote e quelle che finiscono con "X" ed infine rimuovere gli invii a capo e sostituirli con una virgola in modo che tutti i valori siano sulla stessa riga e separati da virgola
for line in $(sed -e '/^$/d' file.txt  |sed -e '/X$/d'); do echo -n $line","; done
e ottengo:
XX13227,4004,x4994,13227,xXX4004,X13227,X4004,4004,
oppure in maniera più elegante
sed -e '/^$/d' file.txt  |sed -e '/X$/d' | tr '\n' ', '

e ottengo comunque

XX13227,4004,x4994,13227,xXX4004,X13227,X4004,4004,

se poi voglio fare il "superfine" e rimuovere l'ultima virgola

sed -e '/^$/d' file.txt  |sed -e '/X$/d' | tr '\n' ', ' | sed 's/\,$//g'

e ottengo:

XX13227,4004,x4994,13227,xXX4004,X13227,X4004,4004

-------------------------------------------

Sempre con sed si può fare un'altra bella cosa. Spesso negli howto si legge: "edita il file tale e sostituisci questo con questo". Sed offre qualcosa di meglio:

echo "ldap=192.168.01" > test 
sed -e "s/192.168.01/192.168.0.1/g" -i test 
cat test
ldap=192.168.0.1



Riferimenti:

Comments

nu

raked append replica mens watches visor coins morse vv

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Internal paths in double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or relative path.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.