extracting text and reusing the text to rename file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extracting text and reusing the text to rename file
# 1  
Old 01-30-2009
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  
Old 01-30-2009
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  
Old 01-30-2009
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
# 4  
Old 01-30-2009
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  
Old 01-30-2009
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  
Old 02-02-2009
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  
Old 02-03-2009
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

3. Shell Programming and Scripting

Extracting and copying text from one file to another

Helooo, So I have a .fasta file (a text file with sequence data) which looks like this, with just over 3 million lines of data. >TCONS_00000001 gene=XLOC_000001 AATTGTGGTGAAATGACTTCTGTTAACGGAGACATCGATGATTGTTGTTACTATTTGTTCTCAGGATTCA... (8 Replies)
Discussion started by: 4galaxy7
8 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

Extracting text from within a section of text using AWK

I have a command which returns the below output. How can I write a script to extract mainhost and secondhost from this output and put it into an array? I may sometimes have more hosts like thirdhost. I am redirecting this output to a variable. So I guess there should be a awk or sed command to... (7 Replies)
Discussion started by: heykiran
7 Replies

6. Shell Programming and Scripting

help extracting text from file

Hello I have a large file with lines beginning with 552, 553, 554, below is a small sample, I need to extract the data you can see below highlighted in bold from this file on the same location on every line and output it to a new file. Thank you in advance for any help 55201KL... (2 Replies)
Discussion started by: firefox2k2
2 Replies

7. UNIX for Dummies Questions & Answers

Extracting the last column of a text file

I would like to extract the last column of a text file but different rows of the text file have different numbers of columns. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

extracting part of a text file

Hi guys So I have a very large log file where each event is logged along with the time that it occurred. So for e.g. The contents of the file look like: ... 12:00:07 event 0 happened. 12:01:01 event 1 happened. 12:01:05 event 2 happened. 12:01:30 event 3 happened. 12:02:01 event 4... (10 Replies)
Discussion started by: alinaqvi90
10 Replies

9. Shell Programming and Scripting

Extracting specific text from a file

Dear All, I have to extract a a few lines from a log file and I know the starting String and end string(WHich is same ). Is there any simplere way using sed - awk. e.g. from the following file -------------------------------------- Some text Date: 21 Oct 2008 Text to be extracted... (8 Replies)
Discussion started by: rahulkav
8 Replies

10. Shell Programming and Scripting

Extracting a line in a text file

If my file looks like this…. 10 20 30 and I want to take each line individually and put it in a variable so it can be read later in it's on individual test statement, how can I do that? I guess what I'm asking is how can I extract each line individually. Thanks (5 Replies)
Discussion started by: terryporter51
5 Replies
Login or Register to Ask a Question