Scripted File Rename

elf_lord_ZC5

Honorary Master
Joined
Jan 3, 2010
Messages
19,860
Reaction score
8,387
Location
Bloemfontein
I have a directory full of text files named with names as follows A51.txt

I need to rename these to something more meaning full to myself and others. These are the lyrics from a Church LED projection board and are I believe related to an indexing function.

The first line is blank.

The second line contains the same value as the file name, as above A51.

The third line contains the title of the lyric in that file Amazing Grace.

What I want is to rename each *.txt file name to show the name of the lyric to Amazing Grace.txt, or Amazing Grace A51.txt - indexing number might still be useful - there might be other versions of Amazing Grace included.

This would greatly help me and others move these lyrics into OpenSong or similar, by identifying those we use most and moving them over first.

PPS - The LED board has died, and our Pastor has replaced it with a video projector, which I think is an improvement, but getting our lyric library over to something we can use is very time consuming.

A script to do the above would ease my work radically.

If some kind soul could help with a solution would be much appreciated.

Thanks
 
for file in `ls`; do
currentname=`echo "$file" | awk -F'.' '{ print $1 }'`
newname=`head -3 "$file" | tail -1`
echo mv "$file" "$newname $currentname".txt
done

Run the above in the directory and check the output. If it looks good, remove the "echo" at the beginning of line 4.
 
honestly I'd read all the txt files into a file first than use the for loop to read the file, that way you won't get .,.. or other dir in your output. otherwise gdots looks good. Or maybe ls *.txt just to narrow the field of what your running through the for loop.
 
This is what I get for gdot's script output ;

H141.TXTXT H141
H142.TXTXT H142
H143.TXTXT H143
H144.TXTXT H144
H145.TXTXT H145
H146.TXTXT H146
H147.TXTXT H147

A bit copied from buffer on Putty
 
My apologies ...

I forgot about the dashes :(


-------------------
A001
ALL WHO ARE THIRSTY
-------------------

1. ALL WHO ARE THIRSTY,
ALL WHO ARE WEAK
COME TO THE FOUNTAIN,
DIP YOUR HEART IN THE STREAMS OF LIFE

2. LET THE PAIN AND THE SORROW,
BE WASHED AWAY
IN THE WAVES OF HIS MERCY,
AS DEEP CRIES OUT TO DEEP,

(WE SING...) COME LORD JESUS, COME
COME LORD JESUS, COME
COME LORD JESUS, COME
COME LORD JESUS, COME

AS DEEP CRIES OUT TO DEEP
AS DEEP CRIES OUT TO DEEP
AS DEEP CRIES OUT TO DEEP

(WE SING) HOLY SPIRIT, COME
HOLY SPIRIT, COME
HOLY SPIRIT, COME
HOLY SPIRIT, COME

("Winds of Worship #12: Live From London")

CCLI no.: 2489542
Words/Music: Brenton Brown
(c) 1998 Vineyard Songs (UK/Eire)
 
On thinking about it a bit, I realized that the row of dashes added an extra line, and thus incremented the value 3, by 1 to 4.

Now I get a few of these;

mv: failed to access ‘ALMAL S\352 HY IS WONDERBAAR, WONDERBAAR\r AA09.txt’: Invalid or incomplete multibyte or wide character
mv: failed to access ‘EK HET U WOORD IN MY HART GEB\352RE\r EA19.txt’: Invalid or incomplete multibyte or wide character
 
Okay, first lesson is, "don't drop a citronella lamp on your keyboard" it will die, your keyboard, that is ... :(

Second lesson is, do not do this on a memory stick, but on a native Linux file system.

That works nice, thanks, now to get rid of the "illegal" characters in the file names, especially in the Afrikaans files - but Uncle Google should provide a solution there. Will go looking.

Many thanks
 
honestly I'd read all the txt files into a file first than use the for loop to read the file, that way you won't get .,.. or other dir in your output. otherwise gdots looks good. Or maybe ls *.txt just to narrow the field of what your running through the for loop.

Thanks, lupus. I also thought about making it "ls *.txt" on the drive home yesterday. Great catch.
 
Code:
p$ ls -1tr
1.txt*
2.txt*
3.txt*
4.txt*
5.txt*
$ cat 1.txt
---------
09 Feb 2016 02:25:38 PM
-------------
Bück 1
$ cat 2.txt
---------
09 Feb 2016 02:25:38 PM
-------------
Title 2
$ for i in `ls *.txt`; do FN=${i%.*}; T=`sed -n '4p' ${i}`; echo "${T/ /_}_${FN}.txt"; done
Bück_1_1.txt
Title_2_2.txt
Title_3_3.txt
Title_4_4.txt
Title_5_5.txt
I did not get an error with "Bück".
Could you copy-paste one of the titles with the issue, directly from the file?
 
Okay, so I kept getting an "unreconised character" in the file names, a square with 4 small squares inside it, that I could not get rid of.

So looking at this lot, it occured to me that these files came of of a WIN98 box, the script being used was reading lines inside the file, then using these to create sensible file names.

Thus I thought, DOS line endings, where causing my issues, so, ran all the files thru dos2unix, then the script, provided here. Bingo, problem sorted. Removed the few accented characters that persisted and 8 question marks, task done and dusted. Then ran the files back thru unix2dos, so that they would display correctly in Notepad, on MS Windows systems.

Thanks folks. :D
 
Top
Sign up to the MyBroadband newsletter
X