Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
File 从gunzip文件中提取特定文件_File_Unix_Awk_Process_Append - Fatal编程技术网

File 从gunzip文件中提取特定文件

File 从gunzip文件中提取特定文件,file,unix,awk,process,append,File,Unix,Awk,Process,Append,如何从数据归档中提取一个特定文件 文件夹/测试文件\u 20160101.zip.gz 文件夹/测试文件\u 20160102.zip.gz . . . 文件夹/测试文件\u 20170630.zip.gz 每个压缩文件夹包含 文件_1_20160101.txt 文件_2_20160101.txt 文件_3_20160101.txt 我需要一个带有file_1_2016_2017.txt的文件,作为所有file_1_1.txt文件的串联,并跳过每个文件的标题行 akshay@db-3325

如何从数据归档中提取一个特定文件


文件夹/测试文件\u 20160101.zip.gz
文件夹/测试文件\u 20160102.zip.gz
.
.
.
文件夹/测试文件\u 20170630.zip.gz

每个压缩文件夹包含

文件_1_20160101.txt
文件_2_20160101.txt
文件_3_20160101.txt

我需要一个带有
file_1_2016_2017.txt
的文件,作为所有
file_1_1.txt
文件的串联,并跳过每个文件的标题行

akshay@db-3325:~$ cd /tmp/

# create sample file
akshay@db-3325:/tmp$ seq 1 10 >file_1_20160101.txt
akshay@db-3325:/tmp$ seq 10 20 >file_2_20160101.txt

# create tar
akshay@db-3325:/tmp$ tar -zcvf test1.zip.gz *.txt
file_1_20160101.txt
file_2_20160101.txt

akshay@db-3325:/tmp$ tar -zcvf test2.zip.gz file_2_20160101.txt 
file_2_20160101.txt

# make sure they are created
akshay@db-3325:/tmp$ ls *.zip.gz
test1.zip.gz  test2.zip.gz

# oneliner
akshay@db-3325:/tmp$ for i in *.zip.gz; do tar -xOzf "$i" --wildcards --no-anchored "file_1_*.txt" >tmpfile 2>>errorfile; [ "$?" -eq 0 ] && awk 'NR>1' tmpfile >>out_file || echo "Filename : $i" >>errorfile   ;  done ; rm tmpfile 

# output
akshay@db-3325:/tmp/testdir$ cat out_file 
2
3
4
5
6
7
8
9
10

# error log
akshay@db-3325:/tmp$ cat errorfile 
tar: file_1_*.txt: Not found in archive
tar: Exiting with failure status due to previous errors
Filename : test2.zip.gz
可读性更好

#!/usr/bin/env bash

# create sample files
seq 1 10 >file_1_20160101.txt
seq 10 20 >file_2_20160101.txt

# sample tar
tar -zcvf test1.zip.gz *.txt
tar -zcvf test2.zip.gz file_2_20160101.txt 

# list them
ls *.zip.gz

# delete if exists before because we are appending
[ -f "errorfile" ] && rm errorfile
[ -f "out_file" ]  && rm out_file

# loop through files, gnu tar is used
for i in *.zip.gz; do 
      tar -xOzf "$i" --wildcards --no-anchored "file_1_*.txt" >tmpfile 2>>errorfile; 
      # if exit status is ok then skip header and append
      # else append name of file to error file
      [ "$?" -eq 0 ] && awk 'NR>1' tmpfile >>out_file || echo "Filename : $i" >>errorfile;  
done  

# clean
rm tmpfile 

是否仅提取文件并将其连接到单个外部文件中,还是就地修改存档文件?将文件连接到单个文件中