CS 279 - Week 11 Lecture 1 - 10-30-12
bash command-line tidbit of the day:
a little MORE history
------------------------------------
* you already know the history command
and that you can type the up-arrow and down-arrow
to scroll through and conveniently redo
previous commands.
* !! redoes the previous command.
!<num> redoes the command with that number <num>
in the history.
!-<num> redoes the command <num> commands
ago.
!<str> redoes the most recent command that
starts with <str>.
!?<str> redoes the most recent command that
CONTAINS <str>.
* if you type (at the command prompt)
a ctrl-r, you can then enter part of a
previous command and the shell will try to
reverse-search for a matching command --
type return when you get to the command you
want.
* you can redo the previous command with a
substitution by:
^what-to-replace^replace-with^
...replaces the first instance of what-to-replace
with replace-with.
^2^4^
...redo the previous command with the first
2 replaced with 4.
* you can get a sed-like substitution with:
!!:s/2/4/
...does the same thing as ^2^4^.
BUT:
!!:gs/2/4/ does the GLOBAL substitution of
replacing every 2 in the previous command
with a 4.
...you can use other !-expressions before
the : also.
for history with:
516 emacs test7.txt
doing:
!516:gs/t/x/
...does the command
emacs xesx7.xxx
BACK to a LITLLE MORE sed:
---------------------------
* MANY nice sed examples at:
http://sed.sourceforge.net/sed1line.txt
* there's a d command!
...it deletes lines.
if you precede it by an "address",
a line number or a regular expression,
it deletes just the line with that number
or the lines that match that regular expression.
(if you give it a range, it deletes those
lines in the range.)
sed -e '/^$/d' try-bang.sh
..would output all non-blank lines of
try-bang.sh (it deletes all blank ones, you
see).
* you can have multiple scripts in a sed command
...separate them by newlines or semicolons.
sed -e 's/#.*//;/^$/d' try-bang.sh
...should output only non-comment non-empty
lines from try-bang.sh.
order matters -- this version will result
in blank lines where entire comment lines
were:
sed -e '/^$/d;s/#.*//' try-bang.sh
* from that same good example source
to change scarlet OR ruby OR puce to red
you can do:
sed -e 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' myfile.txt
selectors in sed commands:
* most sed commands can be preceded by a selector,
where a selector is an "address" or a pair of
"addresses".
...each "address" is a line number or a regular
expression (and you can use $ as the address
of the last line).
* example:
/jam/d # delete each line containing jam
/bar/d # delete each line containing bar
3d # delete the 3rd line (1 is the
# number of the 1st line)
* for a range, put a comma between two
"addresses".
/jam/,/jelly/ - range from a
line containing jam to
the next line after that
line containing jelly,
INCLUSIVE
and then repeat until
end of file
* remember: we are selecting lines
that a script is to be applied to --
so, first and last are included.
* if a line has BOTH jam and jelly,
just that one line is selected.
if a line has jam but no subsequent
line has jelly,
all the lines from jam on are selected.