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.

#  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

{
 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"

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' ?

Here's a little Python version:

 $ 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:

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

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:

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

Shawn I really appreciate your help but as the first post says.

Ahh. That's what I get for not reading the original post carefully.

This is a sample of what I really have on the file there are more spaces than the ones that you can check here.

#  ISSUER CODE: QPPS                              
#  FROM DATE: 20090224                         
#  TO DATE: 00000000                                
#  SEQUENCE NUMBER: 01                                    

cfajohnson posted this script that works fine with only one problem.

{
 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"

I doesn't trim the right side spaces.

Does anyone have another idea of a Bash Script that can work with this. I also have AWK and SED on the server nothing else.

Thanks in advance, I really need a solution ASAP, I had tried many hours trying to trim that space without any luck.

read q a; set -- $a; a=$1
read q b; set -- $b; b=$1

Or:

_trim () 
{ 
    _TRIM=$1;
    trim_string=${_TRIM%%[!${2:- }]*};
    _TRIM=${_TRIM#"$trim_string"};
    trim_string=${_TRIM##*[!${2:- }]};
    _TRIM=${_TRIM%"$trim_string"}
}

read q a; _trim "$a"; a=$_TRIM
read q b; _trim "$b"; b=$_TRIM

Etc., etc, ...

This is the full code of the working script if anyone else is looking for something similar.

#/bin/sh

_trim () 
{ 
    _TRIM=$1;
    trim_string=${_TRIM%%[!${2:- }]*};
    _TRIM=${_TRIM#"$trim_string"};
    trim_string=${_TRIM##*[!${2:- }]};
    _TRIM=${_TRIM%"$trim_string"}
}


{
 IFS=:
 read q a; _trim "$a"; a=$_TRIM
 read q b; _trim "$b"; b=$_TRIM
 read q c; _trim "$c"; c=$_TRIM
 read q d; _trim "$d"; d=$_TRIM
} < HASH.out 



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



cp HASH.out "$filename"

Thanks Johnson