Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory.
I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line with a directory name based on the first letter of the filename. For filenames that are numeric, the directory name is '123'. From this, I can perform a diff in order to give me a manifest of files missing locally to be downloaded using curl or wget.

Suggestions please?

Post sample input and desired output.

Right ok so I have a list of filenames:

100.txt
123.txt
297.txt
597.txt
abc.txt
bcd.txt
fgh.txt
xyz.txt

Desired output:
/123/100.txt
/123/123.txt
/123/297.txt
/123/597.txt
/a/abc.txt
/b/bcd.txt
/f/fgh.txt
/x/xyz.txt

Hi

sed -e 's|^[0-9]|/123/&|' -e 's|^[a-z]|/&/&|'   infile

where infile is the file containing list of filenames.

Guru.

sed 's|.|/&/&|;s|^/[0-9]|/123|' infile

:wink:

perl -plne '/^(.)/ and $x=$1; $x =~ /\d/ ? s/^/\/123\// : s/^/\/$x\//' infile

tyler_durden

I was messing about with awk, but sed is definitely a neater solution IMHO.

Thanks all!