如何在Shell脚本中将文件的第三行读入变量?

7
我将执行一个shell脚本,在其中运行一个sybase查询,结果将保存在一个文件中。
输出如下:
            tablename

            ---------------------------
            result of query executed my case will be an integer returned


            (Number of rows effected ).

我想要做的是在shell脚本中将执行查询的结果(在我的情况下是返回的整数)读入变量中。它将出现在我将Sybase查询输出重定向到文件的第三行。
我该如何做呢?
4个回答

10

你是指这个吗?

x=`sed -n '3p' inputfile`;
echo $x;

编辑

执行以下操作。

thirdline=`sed -n '3p' /home/nmsad/abc.txt`;
echo $thirdline;

是的..我猜我只需要这个。 - Invictus
如果输入非常大,可以使用 sed -n '3{p;q}' - glenn jackman

2
尝试使用如下方式使用sed命令:
variable=$(your_script | sed -n '3 p')

实际上,在脚本中,我编写了Sybase查询,它会在文件/home/nmsad/abc.txt中给我一个结果,我需要提取第三行并将其传递给我的变量。 - Invictus

2
在你的脚本中添加这一行。
cat youroutputfile|awk 'NR==3' >new_outputfile

1
如果输入很大:awk 'NR==3 {print; exit}' - glenn jackman

1
这对我很有用:

thirdline=$(sed -n '3p' filename)

然后运行echo $thirdline


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接