The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting domain names out of a text file totus Shell Programming and Scripting 6 02-11-2009 08:53 AM
Extracting specific text from a file rahulkav Shell Programming and Scripting 8 10-22-2008 10:34 AM
Extracting a line in a text file terryporter51 Shell Programming and Scripting 5 10-13-2008 06:34 PM
extracting unique lines from text file soliberus Shell Programming and Scripting 3 08-22-2008 10:18 AM
extracting a set of strings from a text file Deanne Shell Programming and Scripting 2 09-20-2007 11:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-30-2009
JohnDS JohnDS is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
extracting text and reusing the text to rename file

Hi,
I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file.
eg:
'file.ps' contains following text:
14 (09 01 932688 0)t
the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear elsewhere, not followed by a number (eg.14 (Telefoonnummer)t)
The number can appear several times in the file, so I only would like to use the firts occurrence.

So basicaly it comes to that:
file.ps needs to change to 09 01 932688 0.ps
Can someone help me with that?

Tnx in advance,
John
  #2 (permalink)  
Old 01-30-2009
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
filenames with spaces are annoying ;-)

this will move to names with underscores:

Code:
 mv file.ps $( sed -e 's/ /_/g' -e '/14/s/.*14_(\([_0-9]*\).*/\1.ps/g;q'  file.ps)
if you really want to keep the spaces, this is one way:

Code:
eval mv file.ps $(sed '/14/s/.*14 (\([ 0-9]*\).*/\"\1.ps\"/g;q'  file.ps)
  #3 (permalink)  
Old 01-30-2009
JohnDS JohnDS is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
Hi Tytalus,
thank you for the quick response but it doesn't seem so work. (I might be doing something wrong, since I'm just starting to try this kind of stuff)
The filename has been renamed, but it takes the first line in the file.
Therefore I attached the actual ps file.
What I forgot to mention is that the filename itself is also variable, so I was wondering whether your solution would work as well with the *.ps in stead of the file.ps?

best regards,
John

PS indeed the underscores are a better solution
Attached Files
File Type: ps 0127050_out_0001_0008.ps (1.02 MB, 13 views)
  #4 (permalink)  
Old 01-30-2009
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
oops - my bad..

try:
Code:
mv file.ps $(sed -n -e 's/ /_/g' -e '/14/s/.*14_(\([0-9][_0-9]*\).*/\1.ps/gp' file.ps)
Using this approach you'd prob be best to set up a loop around it e.g.

Code:
for f in *
do
mv $f $(se..../gp' $f)
done
Think that should be about right ...

HTH
  #5 (permalink)  
Old 01-30-2009
JohnDS JohnDS is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
It surely works for one specific file! tnx

Best Regards,
John

Last edited by JohnDS; 02-03-2009 at 06:49 AM.. Reason: the rest of this entry wasn't relevant
  #6 (permalink)  
Old 02-02-2009
JohnDS JohnDS is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
Hi Tytalus,
sorry to bother again.
when I ran the first line in this thread then I get the ps header. When I ran the second post then it worked once.
Today I tried again with the follwing line:
mv 0127050_out_0001_0008.ps $( sed -n -e 's/ /_/g' -e '/14/s/.*14_(\([0-9][_0-9]*\).*/\1.ps/gp' 0127050_out_0001_0008.ps)
and I got the following:
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
Do you notice something strange in the line I typed?
Thanks for the help, I realy appreciate it.
  #7 (permalink)  
Old 02-03-2009
JohnDS JohnDS is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
Hi again
I know why the output is not correct. The file I sent you only had one occurrence of the 14 (09 01 932688 0)t.
The one I want to process got multiple. Is there someone who knows how I get only the first occurrence?
When I just run: sed -n -e 's/ /_/g' -e '/14/s/.*14_(\([0-9][_0-9]*\).*/\1.ps/gp' *.ps then I get the following:
09_01_932699_0.ps
09_01_932699_0.ps
09_01_932699_0.ps
5_.ps
5_.ps
5_.ps
09_01_932699_0.ps
5_.ps
09_01_932699_0.ps
5_.ps
5_.ps
5_.ps
09_01_932699_0.ps
5_.ps

I would like to have only the first 09_01_932699_0.ps

Best Regards,
John
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0