为文件中的行数设置变量

时间:2018-01-10 01:39:30

标签: bash

我正在尝试将文件中的行数提取到变量中,然后在同一个脚本中使用此变量,但它对我不起作用(生成的文件为空但如果我硬编码,则代码有效数字而不是变量) 我有以下代码:

class Component extends React.Component {
  onClick() {
    this.setState({a: 1});
  }

  render() {
    console.log('rendering');
    return (
      <div onClick={() => this.onClick()}>
        <svg>
          <path 
            stroke="blue"
            strokeWidth="10"
            fill="transparent"
            d="M50 10 a 40 40 0 0 1 0 80 a 40 40 0 0 1 0 -80"
            strokeDasharray="251.2,251.2">
            <animate
              attributeType="css"
              attributeName="stroke-dasharray"
              from="0" to="251.2" dur="1s" />
          </path>
        </svg>
        <div id="nonSvgBox"></div>
      </div>
    );
  }
}

ReactDOM.render(<Component />, document.getElementById('app'));

请帮忙!

2 个答案:

答案 0 :(得分:2)

sample_info="sample.txt"
num_of_samples=$(wc -l < "$sample_info")

awk -v num="$num_of_samples" '$8==0 && $10==num' SJ.all > SJ.all.samples
  1. var=value是变量赋值的正确语法(=之前和之后没有空格)

  2. 不要使用大写变量,因为它们可能会与环境或内部shell变量发生冲突

  3. wc -l < file不会在输出

  4. 中包含文件名
  5. awk -v var="$foo"将shell变量$foo的值分配给awk变量var
  6. $8==0 && $10==num如果满足此条件,则执行默认操作 - print $0 -

答案 1 :(得分:1)

我建议使用sed

采用不同的方法
sample_info=sample.txt
NumOfSamples=$(sed -n '$=' <"$sample_info")
# Note you shouldn't use full upper-case identifiers for user variables.

这里会发生什么

  • -n sed选项会抑制每行的默认打印。
  • $查找文件中的最后一行,=打印行号。

最后使用awk,您可以更加惯用

awk -v n=$NumOfSamples '$8==0 && $10==n' SJ.all >SJ.all.samples
相关问题