Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeating last command

Status
Not open for further replies.

arobart

IS-IT--Management
Dec 20, 2000
121
US
Is there a way to repeat part of a command at the unix prompt.
For example in Dos, if you want to repeat most of a command except for a portion, you can right arrow and it will repeat each letter in the command and then you can type in the part you want to change.
Sometimes I need to type in long commands that just have a file name that is different, and it sure would be easier if I didn't have to retype the whole line.

Thanks
Annette
 
In Korn Shell you can do `esc+k` and that will bring up the last command; then also you can do `esc+k` followed by the forward slash and type in any part of the command you typed in previously and it will bring up the last command. Say you typed is ps -ef and then later ps then later ps -el and you wanted to find ps -el; type `esc+k` then `/` then `ps`and enter, then press `n` (next) to cycle through all of the ps commands you typed in until you get to the one you want.

Also, the Korn Shell has a command history and you can type in `history` which will list all commands you typed in, then type `r xxx` where xxx is the number in the column; it was the xxxth command typed.
 
I knew about the esc+k, but let me give you an example of what I'm doing. We receive updated price tapes that we load into the system, but we have to change the manufacturer code. So for each file, I type in:
mv PTSPRLOAD_mfg.dat PTSPRLOAD.dat
mv PTSPRLOAD_mfg.idx PTSPRLOAD.idx

I do this for each manufacturer we are changing. It would be easier if I could bring up the whole command, but just change the "mfg". Is there a way to do that?

Thanks!
 
Hi,

Why don't use a simple loop

for i in PTSPRLOAD*; do
mv $i `echo $i | sed -e 's/_mfg//`
done
 
I never saw that command. How does that work?

Annette
 
Well, first we tell the shell "loop for each file PTSPRLOAD*". Then, we rename each file found to the result of the command between ` ` ...

sed is a stream editor that allows you to perform editing commands on text files "on the fly". It can read in a file or from the standard input. Here, we tell him to read from standard input ($i that is echoed contains filename) and, in this string, to replace (first part of script: 's/) first occurence of "_msg" (second part: /_msg/) with nothing (third part: //) (=suppress). So we get the filename without _msg.

Probably useless if you only have 2 files to rename, but quite nice with x files
 
Thanks! I think I'll try that the next price update I get. I usually have about 4 that I change at a time. If there's a shortcut....I'll look for it!

Thanks again!
 
Scripting would be easier in this case, but if you want to change part of the previous command for one-time use, just use vi keystrokes to edit.

For example, if the command you typed is mv PTSPRLOAD.dat PTSPRLOAD_mfg1.dat and the next command you want to type is PTSPRLOAD.dat PTSPRLOAD_mfg2.dat, do this:

After the first command, type esc+k. Then type l until your cursor is over the 1. Press r and then 2. Then just return.

 
I never thought of using vi keystrokes! That would be easier.
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top