LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-14-2009, 02:01 PM   #1
wademac
Member
 
Registered: Apr 2008
Posts: 43

Rep: Reputation: 19
replace line in CSV file and rename file connected to that name


I have a CSV file with

2100480,,,,,The First File Jun 0309.pdf,
2158030,,,,,Another File Name May 0109.pdf,

etc and more information after but not important

what I would like to do is there is a PDF with this file name as well and I need to replace the "The First File Jun 0309.pdf" with the number id "2100480" in the CSV and than rename the physical file called The First File Jun 0309.pdf to 2100480.pdf

the reference number is the same connected to the same PDF file but they add a date on each file for example I may get some in January

2100480,,,,,The First File Jan 0309.pdf,

and in Febuary get

2100480,,,,,The First File Feb 0909.pdf,

etc..

but in the end I only need one file to over right the last with

2100480.pdf
 
Old 07-14-2009, 03:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
The spec is a bit meager, but this may be what you're after:
Code:
awk -F, '{file[$1]=$6}END{for(i in file){printf( "mv \"%s\" \"%s.pdf\"\n",file[i],i)}}' csv_file|bash


Cheers,
Tink
 
Old 07-15-2009, 11:09 AM   #3
wademac
Member
 
Registered: Apr 2008
Posts: 43

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by Tinkster View Post
The spec is a bit meager, but this may be what you're after:
Code:
awk -F, '{file[$1]=$6}END{for(i in file){printf( "mv \"%s\" \"%s.pdf\"\n",file[i],i)}}' csv_file|bash


Cheers,
Tink
That worked great .. it reads the CSV file and than changed all the physical PDF files to the correct number, but how do I now change the PDF names in the CSV to match those new numbered PDF files?
 
Old 07-15-2009, 01:09 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by wademac View Post
That worked great .. it reads the CSV file and than changed all the physical PDF files to the correct number, but how do I now change the PDF names in the CSV to match those new numbered PDF files?
OK, now I notice/understand the 2nd part of the question :}


Code:
awk 'BEGIN{FS=OFS=","}{file[$1]=$6;$6=sprintf("%s.pdf",$1);print $0>"csv_file.tmp"}END{for(i in file){printf( "mv \"%s\" \"%s.pdf\"\n",file[i],i)};system(" mv csv_file.tmp csv_file " )}' csv_file|bash


Cheers,
Tink
 
  


Reply

Tags
renamescript, replacement



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Need to replace a file with a newer version but keep the old file via a rename Joe_P Linux - Newbie 4 04-30-2009 03:51 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
Erase first line of a csv file imkornhulio Programming 2 02-07-2009 06:28 AM
Convert a number on each line to CSV file OlRoy Programming 9 08-20-2008 11:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration