【问题标题】:Linux Command for test file operations [closed]用于测试文件操作的 Linux 命令 [关闭]
【发布时间】:2013-01-13 08:39:46
【问题描述】:

在 Linux 中,我有很多文件,我需要将所有文件的第 n 行的第 m 个单词连同文件名一起复制粘贴到一个普通的 .txt 文件中。所以我的最终文本文件看起来有点像这样......

<FileName1> <mth word of nth line of FileName1>
<FileName2> <mth word of nth line of FileName2>
.
.
<FileNameN> <mth word of nth line of FileNameN>

谁能告诉我这个Linux命令。 感谢您!!

【问题讨论】:

标签: linux file text


【解决方案1】:
#!/bin/sh
if [ "$#" != 3 ]
then
  echo "$0 [dir] [lineno] [wordno]"
  exit
fi

rm -f a.txt
dir=$1
lineno=$2
wordno=$3

while read -u3 file
do
  read -a words < <(tail -n+$lineno $file)
  echo $file ${words[wordno-1]} >> a.txt
done 3< <(find $dir -type f)

【讨论】:

    【解决方案2】:

    awk怎么样?

    #!/bin/bash
    
    # Usage: <line> <word> <files...>
    
    awk "NR==$1 { print FILENAME \" \" \$$2 }" "${@:3}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      相关资源
      最近更新 更多