LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-09-2013, 01:48 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 39

Rep: Reputation: Disabled
Question 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
 
Old 05-09-2013, 02:05 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,311
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
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.
 
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,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
2 members found this post helpful.
  


Reply


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


All times are GMT -5. The time now is 06:04 AM.

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