« COMANDOS PRACTICOSfew options along to ping »

SED, automatically editor

20.10.14

  14:45:00 by admin, Categories: DICCIONARIO COMANDOS LINUX

If you want to write a program to make changes in a file, sed is the tool to use.

//Buscar y reemplaza y palabra dentro de un archivo

 sed "s/hola/hell/" exam   --> only display result

 sed  "s/hola/hell/" exam > examnew

sed -n /hola/p exam  --> only display lines with "hola"  p(print)

 sed 's/hello/hell/2' file.txt --> remplace second occurrence

sed 's/http:\/\/ /www/' file.txt  --> character scape to "/"  and replace for http:// --> www

 

sed 's/unix/{&}/' file.txt --> display pattern with {pattern}
sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt  --> find (.\) on start each 3 first lines and replace with
nothing


sed -e /fecha_actual/fecha_nueva/s archivo_origina > archivo_editado

 

- a simple example is replace "day"  in the file  "old" to  "nigth" in the dile new

sed s/day/night/ <old >new

 and if you only want testing this

    echo day | sed s/day/night/

-  Is good habit write quotes

sed 's/day/night/' <old >new

 - if you want change pathname /usr/local/bin to /common/bin - you could use the backslash to quote the slash:

 sed 's/\/usr\/local\/bin/\/common\/bin/' <old >new

 -sometime you want add character a one pattern like parenthesis, or other sign or simbol. ,is valid when you know the word.

sed 's/abc/(abc)/' <old >new

- this won't work when you don't know exactly what you will find.
but,if you need add a simbol to a pattern this is solution.
sed 's/[a-z]*/(&)/' <old >new
a example directly on shell
echo "123 abc" | sed 's/[0-9]*/& &/'
here, you will find patern 0 to 9(word) then did replace with space /& &/ and repeat find it.
reply is 123 123 abc

reference

 

http://www.grymoire.com/Unix/Sed.html

No feedback yet

Aqui relato cada una de las experiencias con LINUX

Search

Categories

All LinuXnet

Random photo

Image post

  XML Feeds

powered by b2evolution free blog software