I am trying to make a script in bash that requires the removal of the the file extension from a file name, like the following
original: something.zip
removed version: something
And I was thinking I could use cut for this, but I am worried that a situation could arise where there might be a file name that has more than one period, etc, something similar to the following
something.v2.zip
having said that, I was wondering if anyone had any recommendations as to what I could do to just remove the last period and the text after it from a line of text/filename? any help would be appreciated, thanks!
f=file.zip
echo "${f%.zip}"
file
The '%' is a parameter modifier, it means, delete from the right side of the value of the variable whatever is after the '%' char, in this case, the string .zip. You can make this more general to remove any trailing extension, by using a wild card like
echo "${f%.*}"
file
If you want to remove the from the last period to the end, try this:
$ f=some.thing.zip
$ echo "${f%.*}"
some.thing
Related
I basiclly need to remove some text from file name and there is multiple file. I think I made a mistake. File name was baddly changed eg. 1.9.1.json --> 1form.9.1.json. I expected 1.9.1form.json. Here is code that I tried:
ren *.json ???form.*
I am looking for a code to undo this, and fix of my code that I tried.
Please don't explane it since I am python coder and I even have no idea to print Hello world in batch.
Note: I can't change it one by one since there is fifty files.
I'm trying to create a fairly simple batch file to download a file from our ftp site and store it in a specific directory. I would like to be able to put the batch file in the path so I can call it from anywhere. Currently I have a line like the following:
curl -v -u %FTP_USER%:%FTP_PASS% -Q "TYPE I" -o %OUTPUT_PATH% "ftp://%FTP_HOST%/%JAR_FILE_NAME%"
What I've discovered is that no matter the value of the output path, the file is always written in the current directory, and any path seperators are converted to underscores in the file name. This happens no matter if I use a '\' or '/' and if I try to escape it with double slashes I just end up with two underscores. Quotes around things don't seem to help either.
My question is does the -o option allow for outputting to a folder other than current working dir? I guess I can have the next step in the script to be "move the file to its destination", but that seems really kludgey.
This sounds exactly like a regression (reported here) we unfortunately brought in curl 7.47.0. We hope to release an updated, fixed, version soon and in the mean time you can probably consider downgrading to an earlier version to work-around this annoying issue.
First off I want to say that
-I didnt ever create a batch file yet, but I am really willing to learn
-I am not even sure if what i want to do is possible with a batch file
What i want to do is the following:
I want to replace a number of files of one file type in a folder each with one and the same file of another file type. In doing this, i want the "replaced" files to keep their original name except for the "replacer" file's extension. I am not talking about file conversion, this is about replacing several different files each with one and the same file, so each of them will look the same later, just with different names and the file extension of the "replacer" file. All of the files inside the folder are to be treated this way, there are no exceptions.
So it looks something like this:
Folder 1 Folder 2
10000000.tga------------->10000000.png (looks like replacer.png)
10000001.tga------------->10000001.png (looks like replacer.png)
10000011.tga------------->10000011.png (looks like replacer.png)
I really hope that my description is sufficiently precise, if not so, I am of course willing to give any information needed. I found parts of what i need (e.g. a loop for files in a folder, an order to replace one file with another file) but I am unsure of how to combine them at all, let alone to achieve what I actually wanted to do.
Any help is greatly appreciated :)
for %%i in (*.tga) do (
copy "replacer.png" "%%~ni.png"
del "%%i"
)
see for /? for details about the %%~.. syntax
I receive files which names contain spaces and change every week (the name contains the week number)
IE, the file for this week looks like This is the file - w37.csv
I have to write a script to take this file into account.
I didn't succeed in writing this script.
If I do :
$FILE="This is the file - w*.csv"
I don't find /dir/${FILE}
I tried "This\ is\ the\ file - w*.csv"
I tried /dir/"${FILE}" and "/dir/${FILE}"
But I still can't find my file
It looks like the space in the name needs the variable to be double-quoted but, then, the wildcard is not analysed.
Do you have an idea (or THE answer)?
Regards,
Olivier
echo /dir/"This is the file - w"*.csv
or — you almost tried that —
echo /dir/This\ is\ the\ file\ -\ w*.csv
Use a bash array
v=( /dir/This\ is\ the\ file - w*.csv )
If there is guaranteed to be only one matching file, you can just expand $v. Otherwise, you can get the full list of matching files by expanding as
"${v[#]}"
or individual matches using
"${v[0]", "${v[1]}", etc
First of all, you should not use the dollar sign in an assignment.
Moreover, wildcard expansion is not called in an assignment. You can use process substitution for example, though:
FILE=$(echo 'This is the file - w'*.csv)
Note that the wildcard itself is not included in the quotes. Quotes prevent wildcard expansion.
I suppose my question in not that clear but let me try to explain it here.
Let's suppose I have opened a file named myfilename.java with the below content
public class test{
}
Now, what I want is to replace test with myfilename. Now to get the filename in vim I used :echo expand('%:r') which gave me myfilename.
Now, my question is how to do I use the above output and replace test with it and map it to a key for future use. What I need is may be something like:
:%s/test/above_output_from_command/g
You need to add \= to tell Vim you're trying to call a function:
:%s/test/\=expand('%:r')/g
See :help sub-replace-expression.
For my application, I ended up using a keybound macro (not sure what the VIM) name for this is) that did the edit I want and simply inserted the filename where I wanted it:
:nmap <F5> 0d$:put =expand('%:p:t')^M0ipackage ^[$a;^[
In my case, I wanted to insert the name of the containint directory instead of the file, so I used:
:nmap <F5> 0d$:put =expand('%:p:h:t')^M0ipackage ^[$a;^[
The 0d$ deletes the contents of the current line, :put =expand('%:p:t')^M inserts the filename, and the remaining commands edit the line around the command to produce something like
package containing_directory;
on the current line.