Read File and use contents to rename another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read File and use contents to rename another
# 1  
Old 02-24-2009
Read File and use contents to rename another

Hello guys, thank God that I found this forum.

I hope that someone can help me because I don't have any idea on how to start it. I know that for some of you this is a very simple task but I'm not as advance on shell scripting like many people out there.

I got this file with a permanent filename (RSA_RECORDS.txt) that the content always starts like this.

Code:
#  ISSUER CODE: QPPS                                                                                                                                                                                    
#  FROM DATE: 20090224                                                                                                                                                                                  
#  TO DATE: 00000000                                                                                                                                                                                    
#  SEQUENCE NUMBER: 001                                                                                                                                                                                 
#  RECORD COUNT: 0000000003                                                                                                                                                                             
#  NUMBER OF FIELDS: 015                                                                                                                                                                                
#  DELIMITER: ; 
BLABLABABLABLABLA
BLABLABABLABLABLA
BLABLABABLABLABLA

I need a shell script that can read those first four lines and creates a copy of that same file with the following name.

QPPS_20090224_00000000_01.txt

As you can notice the name is created by using those first four lines of the file.

<ISSUER CODE>_<FROM DATE>_<TO DATE>_<SEQUENCE NUMBER>.txt


The only issue here is that the main file changes everyday and that is the reason that I need a shell script that can dynamically create a copy of that main file with an specific name.

On my Unix server I can see that I have awk and sed support. No perl, python, ruby, etc.

Thanks for your help guys

Last edited by Shark Tek; 02-25-2009 at 01:23 PM..
# 2  
Old 02-24-2009
Code:
{
 IFS=:
 read q a
 read q b
 read q c
 read q d
} <  RSA_RECORDS.txt

filename=${a# }_${b# }_${c# }_${d# }.txt

cp RSA_RECORDS.txt "$filename"

# 3  
Old 02-25-2009
Thanks Johnson


I'm having a small issue.

I need to trim a, b, c, d both sides, I know that the command above trims the first trailing space but I also have a couple of spaces after the word.


Another thing
I'm just curious about this line "read q b"

I understand the read statement and the letter 'b' that appears to be the name of a variable.

But which is the use of 'q' ?

Last edited by Shark Tek; 02-25-2009 at 11:29 AM..
# 4  
Old 02-25-2009
Here's a little Python version:

Code:
 $ cat rename.py
#!/usr/bin/env python

import re

input = open('test.dat' ,'r')
parsePattern = re.compile(r'^.*:\s*(\S+).*\n$')

fileName = []

for line_num in range(4):
    fileName.append(parsePattern.sub(r'\1', input.readline()))

print "-".join(fileName)

Using your sample data as input:

Code:
 $ ./rename.py
QPPS-20090224-00000000-001

# 5  
Old 02-25-2009
I don't have to python on the machine and to have it I have to do a request that will take a very long time.
# 6  
Old 02-25-2009
Quote:
Originally Posted by Shark Tek
I don't have to python on the machine and to have it I have to do a request that will take a very long time.
Okay, here's an ugly Perl one-liner:

Code:
cat test.dat  | perl -ne ' if ($. < 5){$_ =~ s/^.*:\s*(\S+).*\n$/$1/; $newName = $newName . $_ . "-"}else{$newName =~ s/-$//; print $newName; exit;}'

# 7  
Old 02-25-2009
Shawn I really appreciate your help but as the first post says.

Quote:
Originally Posted by Shark Tek
On my Unix server I can see that I have awk and sed support. No perl, python, ruby, etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename file in directory using contents within each file

In the below there are two generic .vcf files (genome.S1.vcf and genome.S2.vcf) in a directory. There wont always be two genaric files but I am trying to use bash to rename each of these generic files with specfic text (unique identifier) within in each .vcf. The text will always be different, but... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

How to read contents in each file and rename the file?

Hello All, Can you help me in writing a script for reading the specific position data in a file and if that data found in that file that particular file should be renamed. Ex: Folder : C:\\test and Filename : CLSACK_112214.txt,CLSACK_112314.txt,CLSACK_112414.txt Contents in the file would... (3 Replies)
Discussion started by: nanduedi
3 Replies

4. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

5. Shell Programming and Scripting

read contents of a file using AWK

Hi, I am kind of new at awk programming, so any help would be great ! I am trying to read a date from a file into a variable and a count into another variable and display both these variables. The file looks like the attached file... I tried this but it doesn't work ... ... (6 Replies)
Discussion started by: RDR
6 Replies

6. Programming

read() contents from a file

Hi, I'm trying to implement a C program on ubuntu which reads the contents of a file that is passed in as an argument and then displays it to the screen. So far I've cobbled together this from bits online but most of it is probably wrong as its all copied and pasted... #include <stdio.h>... (2 Replies)
Discussion started by: cylus99
2 Replies

7. Shell Programming and Scripting

how to read contents of file?

I have made a script something like this. I want it to read the contents of either file or directory but 'cat' and 'ls' is not working. Can anyone help me? I am a newbie in scripting so dont know much about it. I also dont know how can i put my code separatly on this forum #!/bin/bash echo... (9 Replies)
Discussion started by: nishrestha
9 Replies

8. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

9. Shell Programming and Scripting

Read contents from a file

Hi Friends, I am new to this forum. Just struck up with a logic. I have a csv file seperated by ":" (colons). This csv file contains hostname and groups as follows: HOSTNAME:VT Group SGSGCT2AVPX001:Team1 SGSGCT2AVPX003:Team2 SGSGCT2AVPX005:Team2 PHMNCTTAVPX001:Team3 I want to... (2 Replies)
Discussion started by: dbashyam
2 Replies

10. Shell Programming and Scripting

how to read the contents of a file using PERL

Hi My requirement is to read the contents of a fixed length file and validate the same. But am not able to read the contents of the file and when i tried it to print i get <blank> as an output... I used the below satatements for printing the contents ... (3 Replies)
Discussion started by: meva
3 Replies
Login or Register to Ask a Question