-1

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!

adkiem
  • 3
  • 2
  • 1
    do you also want/need to strip the `.txt` from the input file name? or should the output filename also have a `.txt` extension? – markp-fuso Aug 20 '22 at 20:04
  • sorry, that was a typo. I corrected it in the post. The output should be `output_file_123.txt` – adkiem Aug 21 '22 at 12:16
  • And I'm a little said about the -1 because I actually DID put research effort into my question – adkiem Aug 21 '22 at 12:17
  • you should read up about bash programming. to be writing programs like that and not just a single command. then things like this become obvious. – john-jones Aug 21 '22 at 12:21
  • @john-jones: So what is the obvious answer then? Or what would I specifically need to search for regarding bash programming? Bash programming is a brought topic, isn't it? I honestly tried to figure it out for a good amount of time. I also got O'Reillys "Linux in a nut shell" or whatever the english title is and know some stuff. But what I want to do seems to advanced (for me) and too specific to stumble across in a "bash programming beginners how-to". – adkiem Aug 21 '22 at 12:31
  • I think it would be clever for you to read and glossarise, something like this https://www.amazon.co.uk/dp/0596005954?ref_=as_li_ss_tl&language=en_US&ie=UTF8&linkCode=gs2&linkId=84b27c07d94d865bbfb7f412f4c60461&tag=365blottochal-21. and then get used to write an entire script, in which you are running commands like the one you are running. then you could easily use variables instead of strings. And once you are using variables, concatenating them in miscellaneous ways becomes the norm. And you would then have the base string, and concatenate infront of them 'input' and 'output'. – john-jones Aug 21 '22 at 13:57

2 Answers2

2

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}.

IceCode
  • 177
  • 1
  • 13
  • Thanks for the answer. This I knew, but it is supposed to be a one liner I want to fire off from a jupyter notebook using "!". – adkiem Aug 21 '22 at 12:20
0

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}"
markp-fuso
  • 28,790
  • 4
  • 16
  • 36