interesting problem

Hi,

I am new in this forum and I am glad to be a part of it. I have a problem that has two parts:

1/ extract just the timestamp from a filename: for example, I have a file called 'sales20080226144525.txt' and I want to extract just the '20080226144525' part out of it.

2/ Now, take that timestamp and insert it in the beginning of each line with a '|' delimiter. For example,

before:
%> cat test.txt
The cow jumped over the moon
The dish ran away with the spoon

after:
%> cat test.txt
20080226144525|The cow jumped over the moon
20080226144525|The dish ran away with the spoon

Thanks,
CB

TIMESTAMP=`echo "sales20080226144525.txt" | sed "s/^[^0-9]*\([0-9]*\).*/\1/"`

sed -i "s/^/$TIMESTAMP|/" test.txt

For extracting the timestamp, you can use a simpler method, it all depends on the filename format. For example, for the given filename you can also use:

TIMESTAMP=`echo "sales20080226144525.txt" | sed "s/[^0-9]*//g"
echo "sales20080226144525.txt" | sed 's/sales\(.*\)\..*$/\1/'

filename=`echo "sales20080226144525.txt" | sed 's/sales\(.*\)\..*$/\1/'`

sed 's/^/'$filename'|/' input > output

Thanks Robotronic, it works... the power of sed.

CB

Just one comment - for me this is not "interesting problem". Next time try to use more accurate title, like "How to parse and modify a string".
Thanks and sorry for correcting you

just one more way to do this.

 filename=`print "sales20080226144525.txt" | tr -d "A-z."`

agree with prior poster, please use proper title next time.