LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Extract word seperated by slash / (https://www.linuxquestions.org/questions/programming-9/extract-word-seperated-by-slash-4175461245/)

shridhar22 05-09-2013 01:48 AM

Extract word seperated by slash /
 
I give a commond to find a file, eg:.
Code:

find -iname file.po
which gives me a 3 leveled path ie.
Quote:

./path/to/file/file.po
./path2/toThe/file/file.po
Now, this "path" "to" "file" word are to places in a template file which is of the format

Quote:

mypath is '----' '----' '----' 'file' r
The blanks are to be filled by 'path' 'to' 'file' last blank being file always (and not file.po) r remains r always (constant like the name file)
Similarly the next line of template file should be
Quote:

mypath is '----' '----' '----' 'file' r
here the blank should be filed by "path2" "toThe" "file"

and so on

unSpawn 05-09-2013 02:05 AM

You've already been exposed to sed wrt replacing things, for example here: http://www.linuxquestions.org/questi...0/#post4869683 and you've been at LQ long enough to know that you should show some effort yourself. So please show what you came up with.

David the H. 05-10-2013 09:22 AM

1) To start with, let's get the proper syntax for find down:

Code:

find <start_dirs> <global_opts> <matching_opts> <actions>
You need to have one or more starting directories, at the least. The default action is -print, so you don't really need to include it, but it never hurts to be complete.


2) The output of find is likely to be a list of files, not just one, so you really need run a shell loop on the output. And since filenames can include spaces, you need to be able to handle them safely. This means you need to use -print0 null separators.

How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020


3) You need to split the string itself into individual elements. While this can be done with something like sed, it's really easier to just use read with the appropriate IFS setting and output to an array (unless you can guarantee the number of levels you'll get, in which case you can use individual variables).
Since you'll already be using it in the while loop mentioned above, this is the recommended option.

And see here for more on what you can do with shell built-ins:

How do I do string manipulations in bash?
http://mywiki.wooledge.org/BashFAQ/100


All times are GMT -5. The time now is 07:23 AM.