Need a command or script

der.hans PLUGd at LuftHans.com
Tue Dec 2 17:04:54 MST 2008


Am 02. Dez, 2008 schwätzte Dazed_75 so:

> On Tue, Dec 2, 2008 at 4:35 PM, der.hans <PLUGd at lufthans.com> wrote:
>> Am 02. Dez, 2008 schwätzte Dazed_75 so:
>>
>> moin moin,
>>
>>> I know I could figure this out, and will tomorrow if no one gives me
>>> the answer in the next hour.  I have a file with many mv xxx yyy
>>> commands and want to make another file with mv yyy xxx commands so
>>> just need to swap the arguments.  I have to go do some things now and
>>> have a meeting tonight so would appreciate a reminder how to do this.
>>> I know it is trivial but just don't remember how right now.  Thanks in
>>> advance.
>>
>> This command presumes there are no spaces in xxx and yyy and that yyy is
>> the last thing on each command line. Adjust as necessary.
>>
>> $ echo mv xxx yyy | sed -re 's/ ([^[:space:]]+) ([^[:space:]]+)$/ \2 \1/'
>> mv yyy xxx
>>
>> You could also use cut and some variable assignments.
>>
>> ciao,
>>
>> der.hans
>> --
> The source file contains real file names as the xxx and yyy dummys I
> was trying to communicate so they would BE the variable names.
> Basicly the source file contains mv commands to rename a whole bunch
> of files and the file I need to create contains the commands to name
> them back to the original names.
>
> Sorry, I am late and have to run.  Will check email once more before I
> leave.  Your command may well be right but I was surprised to see
> actual xxx and yyys in there.  I haven't used sed in maybe 20 years.

No xxxs and yyys in the command, just in the sample data :).

I should go ahead and handle the mv as well since you've specified it.

$ echo mv fred anke | sed -re 's/mv ([^[:space:]]+) ([^[:space:]]+)$/mv \2
\1/'
mv anke fred

The following looks for mv followed by a space followed by one or more
non-spaces, followed by a space, followed by one or more non-spaces
followed by the end of the line.

It then creates mv followed by a space, followed by the second set of
non-spaces, followed by a space, followed by the first set of non-spaces.

sed -re 's/mv ([^[:space:]]+) ([^[:space:]]+)$/mv \2 \1/'

I just included sample data and output in the original example.

ciao,

der.hans
-- 
#  http://www.LuftHans.com/        http://www.LuftHans.com/Classes/
#  "It's not that I'm so smart, it's just that I stay with problems longer."
#  -- Albert Einstein


More information about the PLUG-discuss mailing list