Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-11-2013, 12:41 PM   #1
LQ Newbie
Registered: Jun 2013
Posts: 1

Rep: Reputation: Disabled
Question File Rename Based on Text Within File in a Particular Location

I have a group of plain text files formatted with the top three lines always in the below format and location. I want the content of the third line starting at character 9 thru the first blank to be the file name. In the below example it is 7027. The line that has 7027 also contains the LastDone content which I want to ignore. I would want to rename this file 7027.txt. I need a command set that will allow me to do that for all files in a directory.

File example:
(this is a blank line)
Site : SITE12 THERE ARE SPACES HERE LastJob : WM02030686
Equip#: 7027 THERE ARE SPACES HERE LastDone :

I am not very good at command structures yet and have spent the better part of two days trying to evolve this myself. I really need a mentoring example.

Last edited by Nargousias; 06-11-2013 at 12:45 PM. Reason: Spaces Missing in example Included THERE SHOULD BE SPACES HERE to represent the spaces.
Old 06-11-2013, 12:45 PM   #2
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,147

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
you could put something like sed -n 3p (man sed) to get the 3rd line and pipe it into something like awk '{print $2}' (man awk) to get the 2nd field and put it in a while loop .

better yet there is a way to do it with a single awk. google: awk, print 3rd line.

edit: if you post in between [code] tags it will preserve spaces.

Last edited by schneidz; 06-11-2013 at 12:59 PM. Reason: op made an edit about spaces.
Old 06-11-2013, 12:55 PM   #3
Registered: Jan 2010
Posts: 65

Rep: Reputation: 21
This should work, untested... coding straight to posting. Assumes your original files are all named *.src and that your matching pattern appears in the first 3 lines of each file's text content

ls -1 *.src| while read f; do new_file=`head -3 "$f"|grep "^Equip#:"|cut -f2 -d" "`; mv "$f" "$new_file.txt"; done
TEST before you commit.

Last edited by thedaver; 06-11-2013 at 12:57 PM.
Old 06-11-2013, 02:50 PM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
That could be made a bit cleaner, sorry to say, and parsing ls in particular is not recommended. A for loop on the globbing pattern is much cleaner and more reliable.

There are also easier ways to extract the desired part of the line:

for fname in *.txt ; do

    newname=$( awk 'NR==3 { print $2 }' "$fname" )
    mv -n "$fname" "$newname"

This is assuming that what you actually want is word 2 on line 3, whitespace-delimited. Let us know if you really need a more complex match.

Also, if that word can be anything other than a simple number or word, you might also want to include a test to ensure that it can safely be used as a filename.


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
how do you rename multiple files based on strings in a text file? Holering Programming 9 01-08-2013 10:22 PM
[SOLVED] how to rename a file based on it creation date limdel Linux - Newbie 11 09-23-2009 06:41 AM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
auto file rename based on time wx_jason Linux - Newbie 10 07-10-2003 11:14 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:55 AM.

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