Home > Mobile >  Bash - How to string replace part of the last argument of the last command when using '>
Bash - How to string replace part of the last argument of the last command when using '>

Time:08-22

in bash I have got something like this:

  • $ <cmd> <arg1> input_file_123.txt > ouput_file_123.txt

I want to reuse input_file_123.txt to name the output file but replace let's say "input" with "output".

I found this: How can I recall the argument of the previous bash command?,

tried this:

  • $ <cmd> <arg1> input_file_123.txt > !$:s/input/output

but it says "substitution failed".

I would need it to be a one-liner I can use within a jupyter notebook.

Thx for your help!

CodePudding user response:

You can save _file_123.txt as a variable: var="_file_123.txt". After that you can call the name by using: input${var} and output${var}.

CodePudding user response:

One option:

# store the input file in a variable

$ infile='input_file_123.txt'

# use parameter substitution to change 'input' to 'output'

$ <cmd> <arg1> "${infile}" > "${infile//input/output}"

Not sure I understand the 'one-liner' requirement but fwiw:

$ infile='input_file_123.txt'; <cmd> <arg1> "${infile}" > "${infile//input/output}"
  • Related