【发布时间】:2016-05-16 17:58:10
【问题描述】:
基本上,我得到的文件将前三列粘贴到后面跟着一列空白行,因为它看起来没有任何内容被附加到第 4 列
我觉得我可能不应该使用我在命令替换中创建的变量,但我不确定如何访问这些我需要的数字
#!/bin/sh # the first file in the expression of a bunch of patients to be made into data files that can be put into the graph
awk '{print "hs"$1,"\t",$2,"\t",$3}' $1 > temp1.txt #important columns saved
numLines=`wc -l $1`
touch column4.txt #creates a column for the average of column 6-
for ((s=0;s<$numlines;s++)); do
currentRow=0 #Will eventually be the average of column 6- for the row of focus
for ((i=6;i<=106;i++)); do
addition=`cut -f $i $1 | head -n $s | tail -n 1` # cuts out the number at the row and column of focus for this loop
currentRow=`expr $currentRow + $addition` # adding the newly extracted number to the total
done
currentRow=`expr $currentRow / 101` #divides so the number is an average instead of a really big number
echo $currentRow >> column4.txt #appends this current row into a text file that can be pasted onto the first three columns
done
paste temp1.txt column4.txt
rm temp1.txt column4.txt
如果它有助于输入文件非常大(大约 106 列和数万行),但这是它的示例
Important identifier line grant regis 76 83 02 38 0 38 29 38 48 (..up to to 106 columns)
another important identifier bill susan 98 389 20 29 38 20 94 29 0 (.. same point)
然后输出看起来像(假设我们排除..之后的列)
Important identifier line 34.88
another important identifier 79.67
对不起,如果有什么不清楚的地方,我尽量说清楚,请问您是否有什么想知道的,我会编辑或评论
谢谢
【问题讨论】:
-
在作业中,删除左侧的
$。 -
numlines与numLines不同。 -
ShellCheck 指出您混淆了
numLines和numlines这两个名称,并且您正在使用带有#!/bin/sh的bash 功能。你能解决这个问题,更新帖子,然后发布你得到的实际输出(或错误)吗? -
wc带有文件名也返回文件名。使用重定向:numlines=`wc -l < "$1"`. -
您的输入文件选项卡是否分开?如果它只使用空格,你需要告诉
cut:cut -d' '
标签: bash shell variables command-line command-substitution