用于测试文件操作的Linux命令

时间:2022-11-13 15:57:22

In Linux, I have many files and I need to copy paste the mth word of the nth line of all the files onto a plain .txt file along with the file names. So my final text file looks somewhat like this...

在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>

Can someone please let me know the Linux command for this. Thanking you!!

有人可以让我知道Linux命令。感谢您!!

2 个解决方案

#1


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


0  

How about using awk?

用awk怎么样?

#!/bin/bash

# Usage: <line> <word> <files...>

awk "NR==$1 { print FILENAME \" \" \$$2 }" "${@:3}"

#1


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


0  

How about using awk?

用awk怎么样?

#!/bin/bash

# Usage: <line> <word> <files...>

awk "NR==$1 { print FILENAME \" \" \$$2 }" "${@:3}"