Unix/Linux Go Back    

UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

[Solved] Search, Extract and Rename Multiple Files

UNIX for Dummies Questions & Answers

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: 8 October 2016, 2:15 AM EDT
Posts: 1,785
Thanks: 97
Thanked 621 Times in 554 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: 21 October 2016, 6:42 AM EDT
Location: San Jose, CA, USA
Posts: 9,530
Thanks: 441
Thanked 3,231 Times in 2,767 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

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 10:55 AM.