Unix正则表达式:遍历文件夹并将文件另存为新名称。

huangapple go评论62阅读模式
英文:

unix regex: loop through folder and save files under new name

问题

I have a folder containing files of the pattern shown below. The plan is to loop through every file name, and match everything between the last / and the . which would match the species name plus underscore in between. But I cannot seem to figure out the right regex.
After the match works, I would like to save the file under a new name, where _sort is appended to the original file name.

The files to loop through:

/home/directory/escherichia_coli.spart
/home/directory/pseudomonas_aeruginosa.spart
/home/directory/pseudomonas_fluorescens.spart
/home/directory/streptocuccus_agalactiae.spart

My most promising attempt:

for i in /home/directory/*.spart; do 
	$i > $i/${i: /$.*\.}_sort.txt; done

/$ to match the last / of the line
.* match everything after the last /
\. match the dot

英文:

I have a folder containing files of the pattern shown below. The plan is to loop through every file name, and match everything between the last / and the . which would match the species name plus underscore in between. But I cannot seem to figure out the right regex.
After the match works, I would like to save the file under a new name, where _sort is appended to the original file name.

The files to loop through

/home/directory/escherichia_coli.spart
/home/directory/pseudomonas_aeruginosa.spart
/home/directory/pseudomonas_fluorescens.spart
/home/directory/streptocuccus_agalactiae.spart

My most promising attempt:

for i in /home/directory/*.spart; do 
	$i > $i/${i: /$.*\.}_sort.txt; done

/$ to match the last / of the line
.* match everything after the last /
\. match the dot

答案1

得分: 2

These are glob patterns, not regular expressions. See perhaps https://stackoverflow.com/questions/23702202/what-are-the-differences-between-glob-style-patterns-and-regular-expressions

Your current code is attempting to run $i as a command and redirect its output to another file. I'm guessing you were looking to copy the file instead.

for i in /home/directory/*.spart; do 
    cp "$i" "${i%.spart}_sort.txt"
done

For your examples, this would produce

cp /home/directory/escherichia_coli.spart /home/directory/escherichia_coli_sort.txt
cp /home/directory/pseudomonas_aeruginosa.spart /home/directory/pseudomonas_aeruginosa_sort.txt
cp /home/directory/pseudomonas_fluorescens.spart /home/directory/pseudomonas_fluorescens_sort.txt
cp /home/directory/streptocuccus_agalactiae.spart /home/directory/streptocuccus_agalactiae_sort.txt

If you really needed to extract just the base name before .spart, the basename command does that, though you can also do it with a second parameter expansion in Bash itself.

for i in /home/directory/*.spart; do 
    base=${i%.spart}
    base=${base##*/}
    echo "$base" should be equivalent to "$(basename "$i" ".spart")"
done

The weird stuff after > in your attempt does not look like Bash syntax at all; are you sure you are not actually using another shell?

英文:

These are glob patterns, not regular expressions. See perhaps https://stackoverflow.com/questions/23702202/what-are-the-differences-between-glob-style-patterns-and-regular-expressions

Your current code is attempting to run $i as a command and redirect its output to another file. I'm guessing you were looking to copy the file instead.

for i in /home/directory/*.spart; do 
    cp "$i" "${i%.spart}_sort.txt"
done

For your examples, this would produce

cp /home/directory/escherichia_coli.spart /home/directory/escherichia_coli_sort.txt
cp /home/directory/pseudomonas_aeruginosa.spart /home/directory/pseudomonas_aeruginosa_sort.txt
cp /home/directory/pseudomonas_fluorescens.spart /home/directory/pseudomonas_fluorescens_sort.txt
cp /home/directory/streptocuccus_agalactiae.spart /home/directory/streptocuccus_agalactiae_sort.txt

If you really needed to extract just the base name before .spart, the basename command does that, though you can also do it with a second parameter expansion in Bash itself.

for i in /home/directory/*.spart; do 
    base=${i%.spart}
    base=${base##*/}
    echo "$base" should be equivalent to "$(basename "$i" ".spart")"
done

The weird stuff after > in your attempt does not look like Bash syntax at all; are you sure you are not actually using another shell?

huangapple
  • 本文由 发表于 2023年4月19日 18:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定