LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-09-2010, 04:36 AM   #1
funonnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Rep: Reputation: 0
How to Read a String and add it at the end in Unix


Hi,

I want to read the line in the text file and get the string in the file and paste it in the directory column

for example i have a line like /ftp/prod/SWC1407.DOC

from this 1407 should be parsed and appended at the end of the move command

mv /ftp/prod/SWC1407.DOC /nfp/1407

could anyone help me in doing this...

Thanks in advance
 
Old 06-09-2010, 05:11 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What have you tried? What language are you using (going to use)? Will the item being found always be numeric?
Will it always be prior to the decimal in the file name?

Basically, we need a little more information
 
Old 06-09-2010, 01:36 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It looks like you are trying to develop a shell script to automate some file moving.
I think what grail is saying is that even though you've given us an example of what you would get from one instance, there is not enough information to determine the pattern you want to use. To get from '/ftp/prod/SWC1407.DOC' to '/nfp/1407' there are many possible transformations that could work. We need you to explain with words what that pattern looks like. For example, "take all numeric characters immediately preceding the .DOC filename suffix and prepend with the directory name 'nfp/'". This would be my first guess at the pattern you want to use, but others would no doubt come up with different ones. What you've given us is a lot like asking "The answer is 7, what is the question?".
When you've thought about the problem as a transformation using patterns of characters, you will be well on the way to defining the solution to the problem. Regular expressions are all about defining and using patterns in the data (not that this problem necessarily requires regular expressions for a solution).
--- rod.

Last edited by theNbomr; 06-09-2010 at 01:37 PM.
 
Old 06-10-2010, 12:14 AM   #4
funonnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for your time guys... and i m sorry for not being clear...

@theNbomr: Yes we are trying to automate a file moving process. we are using shell script for doing this.

my basic aim is to construct the move command with the inputs i am getting like the file location is obtained in the variable i want to use it as the source.

and also i want to get the numeric value before that extension and append it at the end of destination in the move command...

@grail: i am using shell script to do this and yes the value will always be before the decimal point...

thanks...
 
Old 06-10-2010, 02:43 AM   #5
funonnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for your time guys

i got it worked with

$(echo $FILE_NAME|cut -c7-10)

Thank you...
 
Old 06-10-2010, 02:57 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am glad you found an answer, but I thought I would just point out that if your filename changes length, ie more than 10 characters, the above will not work
If you know that they all will be the same then no probs
 
Old 06-11-2010, 05:54 AM   #7
funonnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
@grail: thanks for the info grail. the length of filename wouldn't change but could you tell me how to deal in those cases so that i could learn something
 
Old 06-11-2010, 09:53 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I thought firstly I would have a closer look at your "solution" and it seems it does not work for me at least not the output I would
have expected. Now correct me if I am wrong but from your snippet I have made the following assumptions:

1. Still using original example - /ftp/prod/SWC1407.DOC
2. You have an earlier piece of code stripping the file name from the path

With this in mind I ran the following:
Code:
FILE_NAME=SWC1407.DOC

echo $FILE_NAME|cut -c7-10
The output of this for me is:
Code:
7.DO
Which doesn't really look right to me

But in answer to your question:
[quote]could you tell me how to deal in those cases so that i could learn something[/code]
Here is one possible way:
Code:
var=/ftp/prod/SWC1407.DOC

file_name=$(basename $var)

echo ${file_name//[^0-9]/}
Output for this is:
Code:
1407
 
  


Reply



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
[SOLVED] Why i get a weird symbol at the end of a string? gerardorn Linux - General 8 12-01-2009 05:00 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
gui read out of back end command lines from gui front end activation? how? Siljrath Linux - General 0 10-24-2008 10:11 AM
Using Delimiters to get the end of the string champak Programming 3 12-16-2007 06:15 AM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 07:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration