首页 > 试题广场 >

输出第5行的内容

[编程题]输出第5行的内容
  • 热度指数:50417 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
编写一个bash脚本以输出一个文本文件nowcoder.txt中第5行的内容。

示例:
假设 nowcoder.txt 内容如下:
welcome
to
nowcoder
this
is
shell
code
你的脚本应当输出:
is
示例1

输入

welcome
to
nowcoder
this
is
shell
code

输出

is
head -n 5 nowcoder.txt | tail -n 1
发表于 2020-11-29 23:31:03 回复(3)
sed -n 5p
解析:
$n0 表示当前文件名

如果你只想看文件的前100行,可以使用head命令,如
    head -100 $n0


    如果你想查看文件的后100行,可以使用tail命令,如:
    tail -100 $n0 或 tail -n 100 $n0


    查看文件中间一段,你可以使用sed命令,如:
    sed -n ‘100,200p’ $n0
    这样你就可以只查看文件的第100行到第200行。


发表于 2021-01-21 12:34:07 回复(0)
 sed -n  -e "5,5p" nowcoder.txt
编辑于 2020-11-12 21:11:13 回复(2)
awk 'NR==5{print $0}'


发表于 2022-01-04 10:37:20 回复(0)
awk '{if(NR==5){print $0}}' nowcoder.txt
发表于 2020-12-06 21:33:36 回复(1)
i=0
while read line;
do
    if [ $i -eq 4 ]
    then
        echo $line
        exit 0
    fi
    i=$[$i + 1]
done
发表于 2021-04-19 20:08:21 回复(0)
sed -n '5p'

发表于 2021-01-27 17:16:58 回复(0)
let j=0
for i in `cat nowcoder.txt`
do
    if [ $j -eq 4 ];then
        echo $i
    fi
    let j++
done
发表于 2020-12-27 21:47:58 回复(0)
awk NR==5
发表于 2022-08-01 15:28:36 回复(0)
awk 'NR==5{print $0}'
sed -n  '5p' 
tail -n +5 nowcoder.txt| head -n 1
发表于 2022-07-21 20:17:51 回复(0)
awk 'NR=5 {print $0}'
发表于 2021-09-23 20:08:17 回复(1)
sed -n '5p' nowcoder.txt
发表于 2023-08-16 17:06:41 回复(0)
awk '{for(i=1;i<=NF;i++){if(NR==5){print $i}}}'  nowcoder.txt 
发表于 2023-05-23 11:25:21 回复(0)
cat -n nowcoder.txt | grep '5' | awk '{print $2}'
发表于 2023-03-24 23:00:54 回复(0)
sed -n '5p' nowcoder.txt
发表于 2023-03-02 15:36:33 回复(0)
#!/bin/bash

head -n 5 nowcoder.txt | tail -n 1
发表于 2022-10-26 10:45:14 回复(0)
awk 'BEGIN{ORS=":"}{print $0}' nowcoder.txt |awk -F":" '{print $5}'

发表于 2022-10-25 15:24:01 回复(0)
sed -n '5p' nowcoder.txt
发表于 2022-09-16 18:02:24 回复(0)
sed -n 5p
发表于 2022-08-08 18:05:23 回复(0)
sed -n '5p' nowcoder.txt
发表于 2022-06-29 17:36:44 回复(0)