从文件中存储文本和数字变量以在 perl 脚本中使用 [英] Storing text and numeric variable from file to use in perl script

查看:30
本文介绍了从文件中存储文本和数字变量以在 perl 脚本中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试准备一个与 gnu 并行使用的 bash 脚本.该脚本应采用文件名,将文件名的前缀存储为描述,并将行数 (wc -l) 存储为数字变量.如果这些都成为要在 perl 脚本中使用的变量.描述器工作正常.

I am trying to prepare a bash script for use with gnu parallel. The script should take a filename, store the prefix of the file name as a describer, and store the row count (wc -l) as a numeric variable. Both if these become variables to use in a perl script. The describer is working fine.

但是我对行数的存储,或者我对 ${mm} 的使用没有生成 perl 脚本识别的数字变量.任何更正表示赞赏.

But my storage of the number of rows, or my use of ${mm} is not generating a numeric variable that the perl script recognises. Any corrections appreciated.

#!/bin/bash

# Get the filename and strip suffix
sample=$1
describer=$(echo ${sample} | sed 's/.sync//')
echo ${describer} # works fine

# Get the number of rows
mm=$(cat ${sample} | wc -l)
echo ${mm} # works fine but is this a numeric variable?

# run the script using the variables; 
# the ${mm} is where the perl script says its not numeric
perl script.pl --input ${describer}.sync --output ${describer}.genepop --region ${describer}:1-${mm}

推荐答案

这不是答案.我只是想用更好的 重写你的脚本风格.你知道,你不需要一直用大括号引用变量!例如, $mm 就足够了,在您的情况下不需要 ${mm} .此外,您的 sed 语句可以将注释替换为 等效.我在这里和那里添加了双引号,以便您还可以使用包含空格和其他有趣符号的文件名的所有内容.我还删除了 cat 的无用使用.

This not an answer. I just wanted to rewrite your script in a better bash style. You know, you don't need to refer to variables with curly brackets all the time! E.g., $mm is fine enough, ${mm} is not needed in your cases. Moreover, your sed statement to strip the comment can be replaced with a bash equivalent. I added double quotes here and there so that you'll also be able to use everything with filenames that contain spaces and other funny symbols. I also removed the useless use of cat.

#!/bin/bash

# Get the filename and strip suffix
sample=$1
describer=${sample%.sync}
echo "$describer" # works fine

# Get the number of rows
mm=$(wc -l < "$sample")
echo "$mm" # works fine but is this a numeric variable?

# run the script using the variables; 
# the $mm is where the perl script says its not numeric
perl script.pl --input "$sample" --output "$describer.genepop" --region "$describer:1-$mm"

关于您的主要问题:问题可能出在 程序.

Regarding your main problem: the problem might be in the perl program.

关于你的问题这是一个数字变量吗?,答案是:变量没有类型.它们都是字符串.现在继续阅读:

Regarding your question is this a numeric variable?, the answer is: variables have no types. They all are strings. Now read on:

某些版本的 wc 在它们输出的数字前添加空格,例如,

Some versions of wc add spaces in front of the number they output, e.g.,

$ wc -l < file
      42

(注意 42 前面的空格).您应该能够通过运行我提供给您的脚本版本(带有正确的引用)来注意到您的 wc 版本是否具有这种行为.如果您在数字前面看到一些空格,则可能是导致您出现问题的原因.

(notice the spaces in front of 42). You should be able to notice if your version of wc behaves that way by running the version of the script I gave you (with the proper quotings). If you see some spaces in front of the number, that might be the cause of your problems.

如果是这种情况,您应该替换该行

If this is the case, you should replace the line

mm=$(wc -l < "$sample")

read mm < <(wc -l < "$sample")

希望这会有所帮助!

这篇关于从文件中存储文本和数字变量以在 perl 脚本中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆