سؤال

I want to change the name of the files stored in a series of filelists. This is the code I am using:

suffix=[event]

for i in {0..9} a b; do
    for file in $(cat gsc"${i}".list); do
        echo $file$suffix
    done
done

The output is fine, but the process is much slower than if I just output $file. That is to say, the scripts works fine, but why so slow?

EDIT: probably not clear: I need to change the filename WITHIN the filelist (text file).

هل كانت مفيدة؟

المحلول 4

Superfast and complete:

for i in {0..9} a b; do
    sed -i.bak "s|evt|evt[events]|g" gsc"${i}".list
done

It can be improved (generalized) if the specific string in the sed can be substituted by a general string. I can't do it without avoiding slowness and confusion in the task.

نصائح أخرى

How about this:

suffix=[event]
cat gsc[0-9ab].list|sed "s/$/$suffix/"

If you need change the filename which list in gsc*.list file, try this:

suffix=[event]
cat gsc[0-9ab].list |while read file
do
  mv "${file}" ${file}$suffix
done 

If the lists are large, needlessly expanding them in the backticks could be slowing you down, and have additional problems. Try this instead:

suffix='[event]'

for i in {0..9} a b; do
    while read file; do
        echo $file$suffix
    done <gsc"${i}".list
done

[event] is a shell pattern. When you expand $file$suffix, your shell attempts to match any files in the current directory whose names match the value of $file followed by e, v, n, or t. Quoting the expansion should speed things up, by avoiding the attempt to match the pattern. (Note that you would get very different output if there were any matching files.)

suffix=[event]

for i in {0..9} a b; do
    for file in $(cat gsc"${i}".list); do
        echo "$file$suffix"
    done
done

As others have mentioned, the loop is better written

for i in {0..9} a b; do
    while read file; do
        echo "$file$suffix"
    done < gsc"$i".list
done

or replaced altogether with tripleee's sed command

sed "s|.*|mv '&' '&[event]'|" gsc[0-9ab].list | sh
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top