[mythtv-users] WAaay OT: Just trying to pick the brains of anyone who can help ... Batch txt edit insert title

Simon Hobson linux at thehobsons.co.uk
Wed Jul 13 22:31:30 UTC 2011


Damian wrote:

>Basically, I need to add the title of a text file to the first line of
>the body of the same text file (followed by a couple of carridge returns
>if possible). I then need to do this for about 400 hundreds of files!

I assume by title, you mean file name ?

I'd probably do something like :

#!/bin/bash

source_dir="/some/path/"
dest_dir="/some/otherpath/"

cd "${source_dir}"
for f in *
do
   echo "${f}

" > "${dest_dir}/${f}"
   cat "${f}" ) >> "${dest_dir}/${f}"
done


This (apart from any errors I've probably made) should take each file 
in the source directory, and echo it's file name and a blank line 
followed by it's contents into a new file of the same name in a 
different directory.
As Richard Shaw suggests, you could use the "basename" function to 
strip off file extensions (eg .txt) if required.

This is one of those jobs where there are multiple ways to do it - a 
lot comes down to what tools you are familiar/comfortable with. In my 
case Bash, others will suggest Perl, etc, etc.
But assuming you only want to do this once, it's not worth putting a 
lot of effort into making it "elegant" - just into making to work.

-- 
Simon Hobson

Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
author Gladys Hobson. Novels - poetry - short stories - ideal as
Christmas stocking fillers. Some available as e-books.


More information about the mythtv-users mailing list