Operating System - HP-UX
1752823 Members
4474 Online
108789 Solutions
New Discussion юеВ

How to truncate a file name when copying it

 
SOLVED
Go to solution
June Franduto
Occasional Contributor

How to truncate a file name when copying it

We are writing a script to get some files via ftp which we then want to change the name of before processing. The filenames will be in the format of <1stpart>_<2ndpart>_<3rdpart>_YYYYMMDDHHMMSS.txt
We would like to rename them to the same name minus the HHMMSS.txt. There may be multiple files, the value of HHMMSS will be the time they were created on the remote server. Any suggestions?
9 REPLIES 9
Geoff Wild
Honored Contributor

Re: How to truncate a file name when copying it

Just mv it...

either use awk or cut or sed to parse out the garbage....

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hein van den Heuvel
Honored Contributor
Solution

Re: How to truncate a file name when copying it

I woudl use perl or awk to rename.
For example:


# touch aap_noot_mies_20050404123456.txt
# touch aap_noot_teun_20050404654321.txt
# ls aap*
aap_noot_mies_20050404123456.txt aap_noot_teun_20050404654321.txt
# ls aap* | perl -ne 'chop; rename $_ , $1 if (/(^.*_\d{8})\d{6}(.txt)/)'
# ls aap*
aap_noot_mies_20050404 aap_noot_teun_20050404

The (.txt) makes sure you work on .txt files only allows you to glue that back if you like: rename $_ $1.$2


Hein.

RAC_1
Honored Contributor

Re: How to truncate a file name when copying it

all_char=$(echo "file_name"|awk -F _ '{print $4}'|awk -F . '{print $1}'|tr -d "\n"|wc -c)

chars_to_get=$((${all_char)-6))

new_file_ext=$(echo "file_name"|awk -F _ '{print $4}'|awk -F . '{print $1}'|cut -c1-$)chars_to_get}.txt

Anil
There is no substitute to HARDWORK
June Franduto
Occasional Contributor

Re: How to truncate a file name when copying it

Thank you everyone, we don't generally have a need to write scripts, so no one here has any expertise in this.

I have realized that I mis-stated something, we want to also drop the <3rdpart> of the file when renaming it, I'm trying to decipher the instructions to figure out how to make this work, but I must admit I just don't speak the lingo!
Biswajit Tripathy
Honored Contributor

Re: How to truncate a file name when copying it

Try:

# mv old_name `echo old_name | awk -F_ '{print $1"_"$2".txt"}`

If you want to do this for all the *.txt files in current
directory, then

for old_name in *.txt
do
mv $old_name `echo $old_name | awk -F_ '{print $1"_"$2".txt"}'`
done

Just put the above script in a file and execute it.

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: How to truncate a file name when copying it

Note that both the "mv ..." commands must be in
one single line (my previous was formatted
automatically after posting).

- Biswajit
:-)
curt larson_1
Honored Contributor

Re: How to truncate a file name when copying it

filename="<1stpart>_<2ndpart>_<3rdpart>_YYYYMMDDHHMMSS.txt"

oldIFS="$IFS"
IFS="${IFS}_"
print $filename | read p1 p2 p3 p4
IFS="$oldIFS"

yearMonth="${p4%??????????" #12 ?'s

newFileName="${p1}_${p2}_$yearMonth"
Hein van den Heuvel
Honored Contributor

Re: How to truncate a file name when copying it



In my perl example that rew request would become:

ls aap* | perl -ne 'chop; rename $_ , $1.$2 if (/(^.\w+_\w+)_\w+(_\d{8})\d{6}(.txt)/)'



The regular expression does the work.
It remember 3 elements describe between parens in $1, $2 and $3


(^.\w+_\w+_) = start-of-line plus some-word plus underscore plus some-word --> $1

_\w+ = underscore plus some-word... matched but forgotten

(_\d{8}) = underscore plus exactly 8 decimals --> $2

\d{6} = exactly 6 decimals... match but forgotten

(.txt) = anychar (".") plus "txt"... remembered but unused --> $3

Enjoy.
Hein.

June Franduto
Occasional Contributor

Re: How to truncate a file name when copying it

Thank you very much Hein, that worked beautifully!

Regards,
June