Unix/Linux Go Back    

UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[Solved] Search, Extract and Rename Multiple Files

UNIX for Dummies Questions & Answers

Closed Linux or Unix Question    
Thread Tools Search this Thread Display Modes
Old Unix and Linux 02-26-2014
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
Join Date: Nov 2012
Last Activity: 23 May 2016, 12:54 AM EDT
Location: Dona Paula, Panjim, Goa - 403004
Posts: 1,754
Thanks: 97
Thanked 607 Times in 542 Posts
Try this I modified...

ls *-*-*.csv -1 | \
		awk -F'[-,]' 'FNR==NR{
					gname = substr($2,1,10)
					gsub(/[[:space:]]/,"\\ ",gname)
					A[$1] = gname
                            ($1 in A){
					print "cp "$0,A[$1]"-"$0
				     }' group.csv -  | sh

---------- Post updated Feb 27th, 2014 at 03:37 AM ---------- Previous update was Feb 26th, 2014 at 11:12 PM ----------

I can't produce this Power Too -750416394-RE-file258.csv , there is only one space not two check od -c file
The Following User Says Thank You to Akshay Hegde For This Useful Post:
armsaran (02-27-2014)
Sponsored Links
Old Unix and Linux 02-26-2014
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 29 May 2016, 4:14 PM EDT
Location: San Jose, CA, USA
Posts: 8,732
Thanks: 386
Thanked 2,844 Times in 2,459 Posts
Originally Posted by armsaran View Post

Thanks Don.
Code is not restricted for the first 10 chars
Output file as per your code

Copy of Gardening Category Clickers-750400039-RE-file277.csv

Note that I had posted a fix for this about an hour before you sent this message. I had been working on a solution based on your original requirements and didn't notice your updated requirements until after my initial post. The updated code is:

ls *.csv | awk -F, '
NR == 1 { next }
FNR == NR { c[$1] = substr($2, 1, 10); next }
$1 in c { printf("mv \"%s\" \"%s-%s\"\n", $0, c[$1], $0)
}' Group.csv FS='-' - | sh

The difference between my code and Akshay's code is that my code quotes both operands to mv while Akshay's code changes each occurrence of a <space>, <form-feed>, <carriage-return>, <tab>, or <vertical-tab> character to an escaped <space> character in the destination operand to mv but doesn't protect any characters in class space in the source operand.
Sponsored Links
Old Unix and Linux 02-27-2014
armsaran armsaran is offline
Registered User
Join Date: Feb 2014
Last Activity: 20 March 2014, 6:49 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks Don.

Its working fine
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rename multiple files juzz4fun Shell Programming and Scripting 3 05-20-2013 09:57 PM
[SOLVED] Rename multiple files lost.identity UNIX for Dummies Questions & Answers 8 12-06-2012 06:11 AM
Rename multiple files sylcam Shell Programming and Scripting 4 08-16-2011 01:02 PM
Using AWK: Extract data from multiple files and output to multiple new files Liverpaul09 UNIX for Dummies Questions & Answers 3 10-12-2010 03:59 AM
rename multiple files from search output woodstock UNIX for Dummies Questions & Answers 5 01-06-2009 04:51 PM

All times are GMT -4. The time now is 05:01 PM.