Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存第一行-Linux Shell脚本_Linux_Bash_Shell_Scripting - Fatal编程技术网

保存第一行-Linux Shell脚本

保存第一行-Linux Shell脚本,linux,bash,shell,scripting,Linux,Bash,Shell,Scripting,我有一个文本文件,正在尝试提取文件第一行(或第一行)中的数据,其中每个数据都保存为一个列表(因此每个点都保存在自己的行上),保存在一个新文件中 示例data.txt: Name Col Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Car1 Red 49.3 43.2 54.3 52.3 12.5 76.8 Car2 Blu 56.3 12.4 85.4 67.1 24.5 32.5 and so on.

我有一个文本文件,正在尝试提取文件第一行(或第一行)中的数据,其中每个数据都保存为一个列表(因此每个点都保存在自己的行上),保存在一个新文件中

示例data.txt:

Name  Col  Samp1  Samp2  Samp3  Samp4  Samp5  Samp6
Car1  Red   49.3   43.2   54.3   52.3   12.5   76.8
Car2  Blu   56.3   12.4   85.4   67.1   24.5   32.5
and so on..
我希望新列表如下所示,并保存到名为samps.txt的新文件中:

Samp1
Samp2
Samp3
Samp4
Samp5
Samp6

我对shell脚本非常陌生,可以使用任何人都能提供的所有帮助。

使用
read-a
将行读入,然后使用
for
迭代数组元素。有关详细信息,请参见
帮助。

使用
read-a
将行读入,然后使用
for
迭代数组元素。有关更多详细信息,请参阅
帮助。

这将实现以下功能:

$ head -1 data.txt | grep -o 'Samp[0-9]*'

Samp1
Samp2
Samp3
Samp4
Samp5
Samp6
说明:

  • 显示文件的第一行:
    head-1 data.txt

  • |
    获取上一个命令的输出,并将其用作下一个命令(称为管道)的输入

  • 打印给定的
    regex
    grep-o'Samp[0-9]*'

  • regex
    'Samp[0-9]*'
    将匹配以
    Samp
    开头的任何字符串,后跟任何数字

    要将输出保存到
    samps.txt
    中,请使用重定向操作符

    $head-1data.txt | grep-o'Samp[0-9]*'>samps.txt

    这将适用于任何列标题,而不仅仅是与
    'Samp[0-9]*'匹配的列标题。

    $head-1data.txt | grep-o'\w*'| tail-n+3>samps.txt


    grep-o'\w*”
    匹配单词,并且
    tail-n+3
    显示从第三行开始的所有行(即不显示前两列标题)。

    这将完成以下操作:

    $ head -1 data.txt | grep -o 'Samp[0-9]*'
    
    Samp1
    Samp2
    Samp3
    Samp4
    Samp5
    Samp6
    
    说明:

  • 显示文件的第一行:
    head-1 data.txt

  • |
    获取上一个命令的输出,并将其用作下一个命令(称为管道)的输入

  • 打印给定的
    regex
    grep-o'Samp[0-9]*'

  • regex
    'Samp[0-9]*'
    将匹配以
    Samp
    开头的任何字符串,后跟任何数字

    要将输出保存到
    samps.txt
    中,请使用重定向操作符

    $head-1data.txt | grep-o'Samp[0-9]*'>samps.txt

    这将适用于任何列标题,而不仅仅是与
    'Samp[0-9]*'匹配的列标题。

    $head-1data.txt | grep-o'\w*'| tail-n+3>samps.txt


    grep-o'\w*”
    匹配单词,并且
    tail-n+3
    显示从第三行开始的所有行(即不显示前两列标题)。

    将第一行读入变量

    read -r FIRSTLINE < filename
    
    循环浏览单词并将其输出到文件中

    for WORD in ${WORDS[@]}
    do
      echo $WORD >> outputfilename
    done
    

    在本例中,您希望删除前两列值。您可以在for语句中使用
    ${WORDS[@]:2对数组进行切片。或者,您可以在将值回显到文件之前测试for循环中的值。

    将第一行读入变量

    read -r FIRSTLINE < filename
    
    循环浏览单词并将其输出到文件中

    for WORD in ${WORDS[@]}
    do
      echo $WORD >> outputfilename
    done
    
    在您的情况下,您希望删除前两列值。您可以在for语句中使用
    ${WORDS[@]:2
    对数组进行切片。或者,您可以在将值回显到文件之前测试for循环中的值。

    在处理带有字段的文本文件时,您可能会发现一个有价值的工具:

    awk 'NR==1 { for(i=3;i<=NF;i++) print $i }' file
    
    说明:

    NR is short for the number of rows.
    NF is short for the number of fields in the row.
    
    在处理带有字段的文本文件时,您可能会发现一个有价值的工具:

    awk 'NR==1 { for(i=3;i<=NF;i++) print $i }' file
    
    说明:

    NR is short for the number of rows.
    NF is short for the number of fields in the row.
    
    仅使用bash:

    set -- $(head -1 data.txt)       # save the words in the first line as $1,$2,...
    shift 2                          # discard the first two words
    printf '%s\n' "$@" > samps.txt   # print each remaining word on its own line
    
    仅使用bash:

    set -- $(head -1 data.txt)       # save the words in the first line as $1,$2,...
    shift 2                          # discard the first two words
    printf '%s\n' "$@" > samps.txt   # print each remaining word on its own line
    

    我对伊格纳西奥·巴斯克斯·艾布拉姆斯的答案投了赞成票,因为这是最好的选择,只使用纯
    bash
    。因为他没有给出一个完全有效的例子,这里有一个:

    read -a samps < "myfile.txt"
    printf "%s\n" "${samps[@]:2}"
    

    我对伊格纳西奥·巴斯克斯·艾布拉姆斯的答案投了赞成票,因为这是最好的选择,只使用纯
    bash
    。因为他没有给出一个完全有效的例子,这里有一个:

    read -a samps < "myfile.txt"
    printf "%s\n" "${samps[@]:2}"
    

    您好@Enrico。感谢您迄今为止的帮助。我尝试了您的建议,首先测试了循环中的值,但是没有打印任何内容。脚本似乎正在运行,因为没有错误,但我没有得到所需的输出。有什么建议吗?@ShilaP请确保在ru之前运行了前两个命令来填充Word数组变量nning for循环代码。否则数组是空的,它不会打印任何东西是的,我先运行了前两行,而且似乎没有填充单词?让我们来看看@Enrico。感谢您迄今为止的帮助。我尝试了您的建议,首先测试了循环中的值,但是没有打印任何内容。脚本似乎在运行,正如re没有错误,但我没有得到所需的输出。有什么建议吗?@ShilaP在运行for循环代码之前,请确保您已经运行了前两个命令来填充WORDS数组变量。否则数组为空并且不会打印任何内容是的,我先运行了前两行,看起来WORDS没有填充。让我们来看看