Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal find Command: Manipulate Output String

I am trying to manipulate the filename from the find command:

find . -name "*.xib" -exec echo '{}' ';'

For example, this might print:

./Views/Help/VCHelp.xib

I would like to make it:

./Views/Help/VCHelp.strings

What I tried:

find . -name "*.xib" -exec echo ${'{}'%.*} ';'

But, the '{}' is not being recognized as a string or something...


I also tried the following:

find . -name "*.xib" -exec filename='{}' ";" -exec echo ${filename%.*} ";"

But it is trying to execute a command called "filename" instead of assigning the variable:

find: filename: No such file or directory

like image 937
Mazyod Avatar asked Nov 26 '12 11:11

Mazyod


People also ask

What is string manipulation?

String Manipulation is a class of problems where a user is asked to process a given string and use/change its data. An example question would be a great way to understand the problems that are usually classified under this category.

What is bash string?

Like other programming languages, Bash String is a data type such as as an integer or floating-point unit. It is used to represent text rather than numbers. It is a combination of a set of characters that may also contain numbers.

What is string in shell script?

String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands.

How do you declare a string in bash?

To initialize a string, you directly start with the name of the variable followed by the assignment operator(=) and the actual value of the string enclosed in single or double quotes. Output: This simple example initializes a string and prints the value using the “$” operator.


1 Answers

You can't use Parameter Expansion with literal string. Try to store it in a variable first:

find . -name '*.xib' -exec bash -c "f='{}' ; echo \${f%.xib}.strings" \;

-exec sees first argument after it as the command, therefore you can't simply give it filename='{}' because find doesn't use sh to execute what you give it. If you want to run some shell stuff, you need to use sh or bash to wrap up.

Or use sed:

find . -name '*.xib' | sed 's/.xlib$/.strings/'
like image 155
livibetter Avatar answered Oct 08 '22 17:10

livibetter