Copy and replace content in the filename line

I am trying to copy a file boot-1.5-SNAPSHOT.jar

to boot-1.5.jar

.

I tried this with help sed

, but I am getting an error. I know the syntax is wrong and I hope someone can fill it in for me.

cp boot-* | sed -e 's:-SNAPSHOT::g'
cp: missing destination file operand after β€˜boot-1.5-SNAPSHOT.jar’

      

The idea behind this is that the script will not know the version number, only that the jar will be in the folder where it is trying to copy the file. This is why I am using an asterisk. The version number changes quite often, so using cp file file2

it won't work.

+3


source to share


2 answers


No need for sed

, use Parameter deployment :

$ file=boot-1.5-SNAPSHOT.jar
$ echo ${file/-SNAPSHOT/}
boot-1.5.jar

      

or

$ cp "$file" "${file/-SNAPSHOT/}"

      

In this case:



$ {parameter / pattern / string}

Replacing the template. ... The template is expanded to create as in the path extension. The parameter is expanded and the longest pattern match with its value is replaced with a string.

Note that since you are dealing with files, you must specify variables (otherwise files with whitespace will be corrupted).

It is also possible that a symbolic link should be created instead of a copy:

$ ln -s "$file" "${file/-SNAPSHOT/}"

      

+4


source


You cannot send a command cp

to sed

like this. You must first use sed

in the filename and then make a copy.



f=$(echo boot-*SNAPSHOT.jar)   
cp "$f" $(sed 's:-SNAPSHOT::' <<< "$f")

      

+2


source







All Articles