Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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下获取引用另一个文件的每个创建表的第n个字段?_Linux_Awk_Sed_Grep_Text Processing - Fatal编程技术网

如何在linux shell下获取引用另一个文件的每个创建表的第n个字段?

如何在linux shell下获取引用另一个文件的每个创建表的第n个字段?,linux,awk,sed,grep,text-processing,Linux,Awk,Sed,Grep,Text Processing,我有两个文本文件:file1.txt和file2.txt。 file1.txt是一个索引文件,例如file1.txt的内容: abc 1 def 2 ghi 3 其中“abc 1”是指表“abc”中的第一个字段 file2.txt实际上是许多表的create table命令的转储,例如file2.txt的内容: create table "def".something ( f01 char(10), f02 char(10), f03 char(10),

我有两个文本文件:file1.txt和file2.txt。 file1.txt是一个索引文件,例如file1.txt的内容:

abc 1
def 2
ghi 3
其中“abc 1”是指表“abc”中的第一个字段

file2.txt实际上是许多表的create table命令的转储,例如file2.txt的内容:

create table "def".something 
  (
    f01 char(10),
    f02 char(10),
    f03 char(10),
    f04 date
  );
create table "abc".something 
  (
    x01 char(10),
    x02 char(1),
    x03 char(10),
  );
create table "ghi".something 
  (
    z01 char(10),
    z02 intr(10),
    z03 double(10),
    z04 char(10),
    z05 char(10),
  );
我想根据file1.txt中的每一行“mn”,通过在Linux shell下仅使用或组合使用awk、grep、sed或任何文本处理命令,获得file.txt中表m的第n个字段的列表。关于上述示例,输出预计为:

abc,x01 char(10)
def,f02 char(10)
ghi,z03 double(10)
有可能吗?我该怎么做?

这是您的脚本:

while read line
       do set $line  
       i=$(($2+1)) 
       echo "$1,`grep -A$i "$1" file2.txt | tail -1`"
done < file1.txt 
读取行时
不要设置$line
i=$($2+1))
echo“$1,`grep-A$i”$1“file2.txt | tail-1”
完成
当前输出在第二列中有前导空格和尾随逗号。您可以使用我留给您作为家庭作业的
sed
删除它们

该脚本的工作原理如下: