i want to rename text file with something written in that file
so far i've used grep in the first 20 lines of the text, to find these words (translation from) or (translated from)
i need to take the name after these words (everything until the end of line) and mv it in the filename before extension
This will not work because "{}" can only be used ONCE in an exec clause. If you want this to work you will have to write a small script, like this (stripped down to the bare minimum):
find . -type f -exec grep -wil "\(translated from\)" {} \; -exec mv {} {}.translated\ from \;
i got the right thing regardind to the command and the filename become (les moujiks.txt.translated from)
so the grep & mv commands works well
but i don't know how to move the grep findings (the words after translated from) to the filename to got in final (les moujiks -- translated by denis roche.txt)
les moujiks.txt.translated from
printf "%s\n" *.txt|while read fname
do
if awk 'match($0,/translat(ion|ed) from/){
t=substr($0,RSTART)
gsub(/[^A-Za-z0-9_ -]/,"",t)
if(length(t)){print t;exit(0)}}
END{if(!length(t)) exit(1)}' <(head -20 "$fname") > temp
then
mv "$fname" "${fname%.txt}_$(<temp).txt"
fi
done
If your grep supports the -m and -o switches, you could simplify this. But, then you'll have to make sure that you don't have some "wild" characters in the matching strings.