Tariq
Tariq

Reputation: 9979

Bash: edit file component from variable?

Basically the complete script is all about searching unused images from the xcode project and copy all such images to a temporary directory. Since the script is taking too much time for overall operation so I'm trying to optimize it by reducing operations.

So images are of two types a) sample.png & b) [email protected]

What I'm trying to do is first I'm ignoring @2x.png files in order to reduce number of operations which using this command

for i in `find ../Resources/Assets ! -name '*@2x.png'`; do 

and then by using sample.png I'm searching that file is getting used in the project or not (I'm using ack and other conditions which is working fine). In case the final output is no i.e. image is not getting used in the project then I'm trying to copy that file to a temporary location i.e

cp "$i" "MyFolder"

Now I knew that sample.png is unused so I have to copy [email protected] as well. By doing this way I have avoided number of search and ack operation on @2x file.

But now my issue is how to change the file and copy @2x file as well ?

Upvotes: 1

Views: 72

Answers (2)

sat
sat

Reputation: 14949

You can use this,

cp "$f" "$(sed 's/.png/@2x.png/g' <<< $f)" MyFolder/

(OR)

cp "$i" "${i/.png/@2x.png}" MyFolder/

Upvotes: 2

Austin Hastings
Austin Hastings

Reputation: 627

I wonder if you don't mean to "mv" the files instead, to get them out of the way.

Regardless, you might try:

filename=sample.png
echo "Filename=$filename"
echo "Extra   =${filename/.png/@2x.png}"

Upvotes: 0

Related Questions