LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-28-2011, 12:50 AM   #1
tuxlux
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Rep: Reputation: Disabled
Rename files using csv file and awk


I did some googling and found an awk command to rename files based on a csv file.

Code:
awk -F',' '{print "mv "$1" "$2""}' file_names.csv | /bin/sh
It took me a while to get it to work as I had files with spaces and eventually I just took out the spaces in the csv file (original files were just numbers and no spaces but new names had some spaces). The above command worked except all the files had an extension of .txt with a space or return at the end. I couldn't see it, but it was there and the files couldn't be recognized. Was the problem likely the csv file (each name was on a new line so I guess there was a return at the end of each line) or in the command? If in the csv file, how would you separate each set of names (old.txt,new.txt)? Would you just keep using commas even for end of line and just not have an end of line?

With respect to spaces in file names, the command I found was

Code:
awk -F',' '{print("mv \"" $1 "\" \"" $2 "\"")}' input.csv | /bin/sh
And when I ran it I got

"v

on each line instead of

mv

Any thoughts? Thanks.
 
Old 09-28-2011, 03:43 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
That should not happen with such a simple and working code. Is there a chance you have some hidden and unwanted characters inside input.csv? Has it been created on a windows machine, so that it should be converted to unix format before using it (see the dos2unix command)?
 
Old 09-28-2011, 10:27 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Also, if you show us an example of the csv we might be able to see why the issue is occurring.
 
Old 09-03-2013, 07:45 AM   #4
cannycookie
LQ Newbie
 
Registered: Sep 2013
Posts: 2

Rep: Reputation: Disabled
Cool New Line character

@tuxlux

The issue is that your last variable is including the new line character. Someone more knowledgeable than myself may know how to remove that from the output or you could cheat and add an additional column to the file so that you are not affected by the character.
 
Old 09-03-2013, 10:07 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
@cannycookie - Although not marked as SOLVED, please refer to the date before reviving old questions, ie after 2 years we hope he found an answer
 
Old 09-03-2013, 10:58 AM   #6
cannycookie
LQ Newbie
 
Registered: Sep 2013
Posts: 2

Rep: Reputation: Disabled
resolution

@Grail guru. Don't you find it frustrating when you follow a thread where someone is experiencing the same issue as you and maybe that thread has the answer but then it stops leaving you hanging?

Of course we would expect that 2 years later the guy would have found a solution by now but think about people in the future as well :-)
 
Old 09-03-2013, 08:46 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
hence why you have the ability to raise your own question and reference this one as partial solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
awk with CSV files Jerry Mcguire Programming 5 05-17-2011 07:39 AM
awk and csv file Firebar Programming 9 11-18-2010 06:00 AM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 02:09 PM
AWK: change a particular field in a csv file help help help!!!! haydar68 Programming 20 08-03-2008 02:10 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 10:15 PM


All times are GMT -5. The time now is 08:53 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration