-2

I have a filename that will vary up to the suffix .csv.new. I need to extract the stem. Example file1_test.csv.new. I need to strip out the stem file1_test to create a variable file1_test.err.

closed as unclear what you're asking by John, countermode, Anthon, Anthony Geoghegan, Archemar Oct 21 '16 at 8:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    So... you haven't tried anything? Because you only know pl/sql? That's not how this works. That's now how any of this works. You should start by searching Google for "remove substring in unix". You should have started there before asking this question, honestly, but you should definitely go do that now. – John Oct 5 '16 at 15:46
  • 1
    It's help to know your shell (bash, ksh, zsh, etc) and your operating system-- to focus the answers appropriately. – Jeff Schaller Oct 5 '16 at 15:49
  • 1
    Hang on, do you really want shell variables with variable names? Or do you want to store the name (e.g. file1_test.err) in a variable? – countermode Oct 5 '16 at 21:43
1

basename is a lightweight tool to do what you asked for, e.g.

$ basename file1_test.csv.new .csv.new
file1_test

and thus

var=$(basename file1_test.csv.new).err

where var will contain file1_test.err as desired.

1

Run this. The % makes bash delete the globbing match backwards.

for F in *.csv.new; do
  touch ${F%.csv.new}.err
done

If you have the files

file1_test.csv.new
file2_test.csv.new

you will get

file1_test.err
file2_test.err
  • 1
    The * in ${F%*.csv.new} was superfluous: % deletes the shortest matching substring, and the shortest match for the * wildcard is the empty string. ;) – n.st Oct 21 '16 at 3:11
0

Literally tons of ways. For example sed.

$ ls -1 .
file1_test.csv.new
file2_test.csv.new
file3_test.csv.new
file4_test.csv.new
file5_test.csv.new

$ ls -1 . | sed 's/.csv.new//g'
file1_test
file2_test
file3_test
file4_test
file5_test

Not the answer you're looking for? Browse other questions tagged or ask your own question.