unleashed-web.org

Shell Quick Substitution

| Comments

quick substitution
1
^string1^string2

If you find yourself needing to enter a very similar command multiple times this little command line trick that might save you some time. This is really only helpful if your command line is somewhat long and repetitive.

This is admittedly quite a contrived example but I think it gets the idea across:

example
1
2
3
4
5
6
7
8
9
10
11
12
scotmac:~ shewless$ ls file*.txt
file1.txt	file2.txt
scotmac:~ shewless$ for i in `cat file1.txt`; do echo $i; done
foo
bar
apple
scotmac:~ shewless$ ^file1^file2
for i in `cat file2.txt`; do echo $i; done
baz
qux
banana
scotmac:~ shewless$ 

Step by step:

  • I have two files that I want to apply a similar action to:
list files
1
2
scotmac:~ shewless$ ls file*.txt
file1.txt	file2.txt
  • I will run a command on file1.txt to list all of the lines in the file:
command on file1.txt
1
2
3
4
scotmac:~ shewless$ for i in `cat file1.txt`; do echo $i; done
foo
bar
apple
  • I want to run the same command on file2.txt. Note the only input I have provided is the first line.
command on file2.txt
1
2
3
4
5
scotmac:~ shewless$ ^file1^file2
for i in `cat file2.txt`; do echo $i; done
baz
qux
banana

Enjoy!

Comments