In my previous stone on Terminal tricks using sed, I had included simple sed one-liners to carry out some amazing text editing tasks. The beauty of sed command is more accentuated when used in scripts.A combination of two or more commands with sed can result in effective scripts.

You can either create a bash file or make an alias. Alias allows you to define ‘shortcuts’ to shell commands.

Usage :   alias newcommand=’yourcommand -arguments’

For e.g. in Ubuntu, you can add aliases in .bash_aliases file found in your home directory.

Print the directory structure

The following alias will print the directory structure from the current directory in tree format.

alias dirf='find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"'

Steps:
1. Add these commands in .bash_aliases file found in your home directory.
2. If the file .bash_aliases does not exist, create one and add the commands.
3. Restart the terminal.
4. Type ‘dirf’.


Copy and paste

The following commands allow you to copy files to a temporary directory and paste it to the current directory. It is indeed a handy script as it behaves much like copy and paste feature of GUI.

ccopy(){ cp $1 /tmp/ccopy.$1; }        # copy any file to a file /tmp/ccopy.filename
alias cpaste="ls /tmp/ccopy* | sed 's|[^\.]*.\.||' | xargs -I % mv /tmp/ccopy.% ./%"    
# paste the copied file to current directory

Steps: Same as above.
Usage: Suppose my test directory has files help.c output.c

rabi@rabi:~/test$ ls
help.c  output.c
rabi@rabi:~/test$

Suppose I want to copy help.c in the temp directory so that i can paste it somewhere else later on.

rabi@rabi:~/test$ ccopy help.c

Now, it will copy the file to /tmp directory. So if you type ls /tmp then it will list the copied file as ccopy.help.c

Let us change current directory to /home/rabi/sedtricks/

rabi@rabi:~/test$ cd ..
rabi@rabi:~$ cd sedtricks
rabi@rabi:~/sedtricks$

Now paste the copied file by typing cpaste
rabi@rabi:~/sedtricks$ cpaste

When you list the files ( ls ) then you will find help.c in the current directory. You can perform this for many files too.


Mass-renaming files

The following will rename all the files with extension .text to .txt

find -name "*.text" | sed 's/\(.*\).text$/mv "&" "\1.txt"/' | sh

Usage:
1. Create any bash file ( for e.g. massrenaming.sh )
2. Add #!/bin/bash to the file massrenaming.sh
2. Add the given command to massrenaming.sh
3. Make it executable file

$ chmod +x massrenaming.sh

4. Run the file

$ ./massrenaming.sh

Commas to all numeric strings in a file

The following command will add commas to all numeric strings in a file, changing “1234567″ to “1,234,567″.

sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' filename

Usage:
1. Create any bash file ( for e.g. commainnumbers.sh )
2. Add #!/bin/bash to the file commainnumbers.sh
2. Add the given command to commainnumbers.sh
3. Make it executable file

$ chmod +x commainnumbers.sh

4. Run the file

$ ./commainnumbers.sh

Note: You can make your own scripts with different sed one-liners that i had listed in my previous article as
per your convenience.


Remove numbers from history

The following command to gives a history listing without the numbers.

history | sed 's/^[ 0-9]* //'

Usage:
1. Create any bash file ( for e.g. test.sh )
2. Add #!/bin/bash to the file test.sh
2. Add the given command to test.sh
3. Make it executable file

$ chmod +x test.sh

4. Run the file

$ ./test.sh

Source : http://www.shell-fu.org/lister.php?tag=sed


Share