Help required with a file rename shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required with a file rename shell script
# 8  
Old 06-25-2019
The probelm with a *${var}_* glob is that it is missing a leading anchor.
E.g. $var==2795 will match 002795_001.jpg and 012795_001.jpg Smilie
Reason enough to go for the less efficient but correct `ls | grep` and a ^0* anchor in the RE.
This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 06-26-2019
Thanks to everyone, great solutions, and discussion. I'll try all of these out and let you know if I need any further help. I'll also post here my modified script which will go into production. Apologies again for the initial confusion. Have a great day ahead.
# 10  
Old 07-24-2019
Quote:
Originally Posted by MadeInGermany
The probelm with a *${var}_* glob is that it is missing a leading anchor.
E.g. $var==2795 will match 002795_001.jpg and 012795_001.jpg Smilie
Reason enough to go for the less efficient but correct `ls | grep` and a ^0* anchor in the RE.
I was thinking that bash Extended globbing and nullglob could be the solution to this, without the efficiency hit:

Code:
awk '
NR >= 2 {
  CMD="bash -O extglob -O nullglob -c \"printf \\\"%s\\n\\\" *(0)" $3 "_*.jpg\""
  while (CMD | getline FN && length(FN)) {
...

However, this still ends up invoking sh and then bash, which is only slightly better than sh ls and grep. The complexity (all those backslashes!) and reliance on bash make this more an academic solution rather than something I'd be inclined to use in the real world.
This User Gave Thanks to Chubler_XL For This Post:
# 11  
Old 08-26-2019
It worked - Require one small modification

Quote:
Originally Posted by RudiC
No surprise. Your post #1 input sample provided the wrong structure, contained wrong data, and the sample file names didn't reflect reality either. What would you expect? With your "revised" data and input, try
Code:
awk '
NR >= 2 {("ls -x *" $3 "*.jpg") | getline L
         gsub (/[       ]+/, ",", L)
         LB = L
         gsub (/0+|.jpg/, "", L)
         gsub ($3, $2, L)
         $0 = $0  L
              split (LB, T1)
         n  = split (L,  T2)
         for (; n; n--) OUT = OUT sprintf ("echo mv -- %s %s\n", T1[n], T2[n] ".jpg")
        }
1
END     {system (OUT)
        }
' FS=, file
Full Registration,VIN,Stock ID,Mileage,InternalTrim,Description,Warranty,FranchiseApproved,RegistrationDate,Featured,NewVehicleType,Used stock images
2653BA,WDD1173461N6437866,2795,19434,Leather trim,A Class,Y,Y,19/09/2018,N,CAR,WDD1173461N6437866_1,WDD1173461N6437866_2,WDD1173461N6437866_3
8874MS,WDD1173461N6494217,2745,15452,Leather trim,A Class,Y,Y,19/09/2018,N,CAR,WDD1173461N6494217_1,WDD1173461N6494217_2
mv -- 002795_003.jpg WDD1173461N6437866_3.jpg
mv -- 002795_002.jpg WDD1173461N6437866_2.jpg
mv -- 002795_001.jpg WDD1173461N6437866_1.jpg
mv -- 002745_002.jpg WDD1173461N6494217_2.jpg
mv -- 002745_001.jpg WDD1173461N6494217_1.jpg


The first three lines are your target .csv file printed to stdout. The remaining 6 lines are the test output from the system call; remove the echo from the sprintf command to really have the files renamed.




EDIT: Slight simplification:



Code:
awk '
NR >= 2 {CMD = "ls *" $3 "*.jpg"
         while (1 == CMD | getline FN)
                {FNN = FN
                 gsub (/0+|.jpg/, "", FNN)
                 gsub ($3, $2, FNN)
                 $0 = $0 FS FNN
                 OUT = OUT "echo mv -- " FN " " FNN ".jpg\n"
                }
         close (CMD)
        }
1
END     {system (OUT)
        }
' FS=, file

Hi RudiC,

I used your simplified method and it worked fine, only one trouble I have is that the script doesn't work if stock ID has "0" in it, for example, it will not work if the image file name is "002501_001.jgp" please note that from character 3rd to 6th character is stock ID and can contain "0" in it, can you please modify your provided script to cater to this need? This will be a really great help. Many thanks in advance.

Regards,
Tayyab

Last edited by tayyabq8; 08-26-2019 at 04:41 AM..
# 12  
Old 08-26-2019
Would it suffice to "anchor" the gsub search pattern like
Code:
gsub (/^0+|.jpg/, "", FNN)

? Be aware that the files' sequence number will have leading zeroes, then.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 08-26-2019
Hi RudiC,

Yes I thought about it and yes anchoring alone will not help as the sequence numbers will have leading zeros as mentioned by yourself, any other advice, please. Many thanks once again.

Regards,
Tayyab
# 14  
Old 08-26-2019
OK, add
Code:
sub  (/_0+/, "_", FNN)

just below the first gsub.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script required

Hi, I need shell script for getting the date in format from below text output IP IS 10.238.52.65 pun-ras-bng-mhs-01#show conf port 2/4 Building configuration... Current configuration: ! card ge3-4-port 2 ! port ethernet 2/4 no shutdown encapsulation dot1q (7 Replies)
Discussion started by: surender reddy
7 Replies

2. Shell Programming and Scripting

help required with shell script

Hi, My input file as follws $ cat 1.txt ------- a aa aaa 11 b bb bbb 22 I am able to extract first and last column of a given line as follows. $ nawk '{print $1}' FS= RS= 1.txt | awk '{ $NF = ""; print }' a $ nawk '{print $1}' FS= RS= 1.txt | awk '{ print $NF}' 11 however, the... (4 Replies)
Discussion started by: bala123
4 Replies

3. Shell Programming and Scripting

Shell script is required

Dear All I have a filelisting as below: abcd_20110715_0007 abcd_20110715_0010 abcd_20110716_0001 abcd_20110716_0004 abcd_20110715_0008 abcd_20110715_0011 abcd_20110716_0002 abcd_20110716_0005 abcd_20110715_0009 abcd_20110715_0012 abcd_20110716_0003 abcd_20110716_0006 ... (3 Replies)
Discussion started by: at1700
3 Replies

4. Shell Programming and Scripting

write shell script to rename file

hi, I need some help in writing shell script in a bourne shell.I am trying to rename the file.. eg. find /root/data -type f -name "text*) | while read FILES do newfile=${FILES/type_2.0_20101208_34.xml / tmp.xml} mv "$FILES" "$newfile" done above written script is working...If the... (7 Replies)
Discussion started by: shubhig15
7 Replies

5. UNIX for Advanced & Expert Users

Rename a file to a file_current datetime in a shell script

Hi all, Could anyone suggest me on Renaming a file to a file_current datetime in a shell script. (3 Replies)
Discussion started by: Nithin
3 Replies

6. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

7. Shell Programming and Scripting

Shell script help required

Hi, Can someone help me with this small piece of code. DIRNAME=$(dirname $0) BASENAME=$(basename $0) DATA="${DIRNAME}/${BASENAME}.data" && . $DATA whats is meant by && . $DATA here... Regards, Abhishek (2 Replies)
Discussion started by: max29583
2 Replies

8. Linux

shell script required

Hi, iam presenting the input text file format.Of this i need the character count of the number of characters present in each file.The attached file is a combination of 3 text file.each text file starts at record 1 - 34, then the next tetx file starts. What i need is the character count of each... (1 Reply)
Discussion started by: sethunath
1 Replies

9. Shell Programming and Scripting

shell script required

hi , i need a shell script that will remove the first and second lines of the text file and will list the word count of the characters present in it. the text file will be consisting of multiple textfiles.the first text file starts from 01-34.like wise the next file also starts from 01-34... (4 Replies)
Discussion started by: sethunath
4 Replies

10. Shell Programming and Scripting

Urgent help required in deleting a line without opening a file usinga shell script

Hi, I need a help in deleting a line matching a particular pattern in a file using shell script without opening the file. The file is a .c/.cpp file. Is it possible? Thanks (6 Replies)
Discussion started by: naan
6 Replies
Login or Register to Ask a Question