LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-10-2014, 02:27 AM   #1
beast12
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Rep: Reputation: Disabled
Bash Script renaming


Hello all!

I would like to rename txt files in a folder to the last word of the first line IN the txt file.
This is what I have now:

Code:
name_check=$(cat info*.txt | head -1 | awk '{ print $NF }')

for filename in info*
do
        mv $filename $name_check
done
The info*.txt are the orignal files. They originate from a api call from ovirt. So I really need to change the file names info*.txt to the server name which is located on the first line as extracted with the name_check. With all my fingers crossed :-), I hope somebody can help me out here :-)
 
Old 01-10-2014, 02:34 AM   #2
allez
Member
 
Registered: Jul 2008
Location: Russia/Siberia/Krasnoyarsk
Distribution: SuSE, CentOS, FreeBSD
Posts: 77

Rep: Reputation: 21
You have to put getting the new filename inside the loop:
Code:
for filename in info*
do
  name_check=$(head -1 "$filename"| awk '{ print $NF }')
  mv -v "$filename" "$name_check"
done
 
Old 01-10-2014, 02:39 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So we got rid of the useless cat, lets do the same with head:
Code:
awk 'NR==1{print $NF}' "$filename"
 
1 members found this post helpful.
Old 01-10-2014, 02:39 AM   #4
beast12
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Indeed! That was kinda stupid from me :-D.

But BIG THANX!! You are my hero for today!!! :-)
 
  


Reply



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
Bash script for renaming newest file in folder Stepo Programming 7 08-02-2012 07:08 AM
Bash script for renaming files (all odd) snaggletooth1134 Programming 10 05-01-2012 12:26 AM
[SOLVED] Linux issue renaming files - BASH script giantanakim Programming 25 11-01-2011 12:21 PM
Renaming MP3 collection [bash script] mainstream Linux - General 7 12-22-2010 06:29 PM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 AM.

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