Unix File name manipulation


 
Thread Tools Search this Thread
Operating Systems AIX Unix File name manipulation
# 1  
Old 09-29-2011
Error Unix File name manipulation

I need a script that will raname the following file names that beging with 08078* in unix as follows:

Rename 08078-08201103-H00044-CA.835
as follows:

08078-110820-H000440CA.835

Bascially it will do this:

1) Keep the first 6 positons.
2) Move the yr from the file name to be the first after the first hyphen
to be like this 110820 "yymmdd"
3) Remove the 03 after 11 in the file name so the final file name is 08078-110820-H00044.CA.835


Please let me know as soon as possible. I am pressed for time and hope to have an anser to today if possible. Thank you
# 2  
Old 09-29-2011
What have you tried so far & where are you stuck? This is a help and support forum, not a solution-by-demand site.
# 3  
Old 09-30-2011
Bug

I figured it out on my own after I posted this. It is working. Here is how I did it:

I have files coming like this: 08101101.wsp ...etc I finally got it to work to write out the file per specs as:
Code:
    08078-110810-H00044-C01W.835

and her is the code I came up with and it works:
Code:
GetFileName=08
 
#First loop to add 08078 at the begining of the file and and H00044-C.835 # at the end:
for file in $GetFileName*.*; do
     newfile=`echo 08078-$file-H00044-C.835`
     mv $file ./$newfile
done
 
# 2nd For loop to only keep one extention on the file name
for file in 08078-*.835; do
     newfile=`echo $file | awk -F'.' 'BEGIN{OFS=""}{s=$NF;o=$0;$NF="";print "mv "o" " $0"."s}'|sh`
done
 
# 3rd For loop to re-arrange how to rename the file:
for file in 08078-*.835; do
     GetNumb=`echo $file  | cut -c1-5`
     Getmmdd=`echo $file  | cut -c7-10`
     GetYear=`echo $file  | cut -c11-12`
     GetSeq=`echo $file   | cut -c13-15`
     GetIns=`echo $file   | cut -c19-24`
     GetChar=`echo $file  | cut -c26-26`
     GetRest=`echo $file  | cut -c 27-`
     NewFile=$GetNumb-$GetYear$Getmmdd-$GetIns-$GetChar$GetSeq$GetRest
     mv $file ./$NewFile
done

It works for what I want it to do. Is there a better way to do this "Maybe with one for loop vs. 3 loops ?



Thanks


Moderator's Comments:
Mod Comment Please use code tags

Last edited by zaxxon; 10-10-2011 at 05:26 AM.. Reason: code tags, see PM
# 4  
Old 10-07-2011
Code:
#! /usr/bin/ksh
###############

ls | grep "^08078" | while read org_name ; do
        print "Org name  : $org_name"

        ##
        ## Split filename in separate parts
        ##
        tmp=$org_name
        pre=${tmp%%-*}

        tmp=${tmp#*-}
        date=${tmp%%-*}

        tmp=${tmp#*-}
        string=${tmp%%-*}

        tmp=${tmp#*-}
        code=${tmp%%.*}

        post=${tmp##*.}


        ##
        ## Split Date in Year Month and Day
        ##
        year=${date%??}
        year=${year#????}

        month=${date%??????}

        day=${date#??}
        day=${day%????}

        ##
        ## Define new name
        ##
        print "New name  : $pre-$year$month$day-$string$code.$post"
done

Code:
Org name  : 08078-08201103-H00044-CA.835
New name  : 08078-110820-H00044CA.835

# 5  
Old 10-07-2011
MySQL

Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Manipulation - UNIX script

Hi, I have a file about 100 lines. Each line is about 2000 characters (each line is fixed length). In middle of each line is following constant value 0000040029892586 Now, I want to go through each line and increment by 1. So, line 1 will have 586 line 2 will have 587, line 3 will have... (4 Replies)
Discussion started by: jakSun8
4 Replies

2. Shell Programming and Scripting

Stream manipulation in UNIX shell scripting

i have a file something like this : start: 01:00:00 01:30:00 02:30:00 05:30:00 end: 01:13:00 02:00:00 02:40:00 05:45:00 and i want (end - start) total run time in below format: run: 00:13:00 00:30:00 00:10:00 00:15:00 (4 Replies)
Discussion started by: Acme
4 Replies

3. Shell Programming and Scripting

Manipulation of file data with UNIX

Hello , How all doing today.. I have a little doubt in Unix (6 Replies)
Discussion started by: adisky123
6 Replies

4. Shell Programming and Scripting

Populating File data with custom manipulation on file names

Hi, I am confused how to proceed firther please find the problem below: Input Files: DCIA_GEOG_DATA_OCEAN.TXT DCIA_GEOG_DATA_MCRO.TXT DCIA_GEOG_DATA_CVAS.TXT DCIA_GEOG_DATA_MCR.TXT Output File Name: MMA_RFC_GEOG_NAM_DIM_LOD.txt Sample Record(DCIA_GEOG_DATA_OCEAN.TXT):(Layout same for... (4 Replies)
Discussion started by: Arun Mishra
4 Replies

5. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

6. UNIX for Dummies Questions & Answers

Filtering records from 1 file based on some manipulation doen on second file

Hi, I am looking for an awk script which should help me to meet the following requirement: File1 has records in following format INF: FAILEd RECORD AB1234 INF: FAILEd RECORD PQ1145 INF: FAILEd RECORD AB3215 INF: FAILEd RECORD AB6114 ............................ (2 Replies)
Discussion started by: mintu41
2 Replies

7. UNIX for Dummies Questions & Answers

UNIX - File/Table/Data manipulation

Hi, I have a table (e.g.): a 1 e 4 5 6 b 2 r 4 4 2 c 5 r 3 7 1 d 9 t 4 4 9 . . What I need to do is to set the values of some values in column 2 to negative values. For example, the values 2 and 9 should become -2 and -9 in the modified file. How should I go about... (2 Replies)
Discussion started by: pc2001
2 Replies

8. UNIX for Dummies Questions & Answers

String manipulation in Linux & Unix (Solaris)

Guy I have this below report and I need someone help me formatting it SID File Name Started Finished Backup ABC XYZ.log 10Sep09-012857\n 10Sep09-020748 Successful I need Started & Finished columns to be formatted as ... (1 Reply)
Discussion started by: anjum.suri
1 Replies

9. Linux

string manipulation in unix

Hi, I have a question . I have script which gets info from db2 engine. The script is cat sample_substitute.sh CNTR_NM=`db2 -x "select CONTAINER_NAME ,usable_pages from table( SNAPSHOT_CONTAINER('TST103',-1)) as SNAPSHOT_CONTAINER, syscat.tablespaces where tablespace_name = tbspace and... (3 Replies)
Discussion started by: capri_drm
3 Replies

10. Shell Programming and Scripting

Unix Shell Novice - File Manipulation

Hi, I am brand new to unix and am hoping someone can start me in the right direction. I want to be able to put the results of a file command such as wc -l filename into a variable which I can then use to test against another variable...i.e. I need to show the nth line of a file, but need to... (3 Replies)
Discussion started by: nmullane
3 Replies
Login or Register to Ask a Question