Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-09-2013, 01:48 AM   #1
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Question Extract word seperated by slash /

I give a commond to find a file, eg:.
find -iname file.po
which gives me a 3 leveled path ie.
Now, this "path" "to" "file" word are to places in a template file which is of the format

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
mypath is '----' '----' '----' 'file' r
here the blank should be filed by "path2" "toThe" "file"

and so on
Old 05-09-2013, 02:05 AM   #2
Registered: May 2001
Posts: 29,359
Blog Entries: 55

Rep: Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546
You've already been exposed to sed wrt replacing things, for example here: 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.
Old 05-10-2013, 09:22 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

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

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?

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?
2 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl word extract sunlinux Programming 2 06-22-2012 12:08 PM
how to extract numbers from a word using sed radhikamody Linux - Newbie 5 02-26-2012 12:05 PM
[SOLVED] How to extract the first word following a string Feynman Linux - Newbie 24 08-24-2010 12:27 PM
How to extract text blocks seperated by blank line art84_LV Programming 9 11-17-2009 01:02 AM
extract text only from ms word in php sriphp Linux - Newbie 2 05-11-2009 01:07 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration