bash - Make cat command to operate recursively looping through a directory -
i have large directory of data files in process of manipulating them in desired format. each begin , end 15 lines soon, meaning need strip first 15 lines off 1 file , paste them end of previous file in sequence.
to begin, have written following code separate relevant data easy chunks:
#!/bin/bash destination='media/user/directory/' file1 in `ls $destination*.ascii` echo $file1 file2="${file1}.end" file3="${file1}.snip" sed -e '16,$d' $file1 > $file2 sed -e '1,15d' $file1 > $file3 done
this worked perfectly, next step worlds simplest cat
command:
cat $file3 $file2 > outfile
however, need stitch file2
previous file3
. look @ screenshot of directory better understanding.
see how these files sequential on time:
*_20090412t235945_20090413t235944_* ### april 13 *_20090413t235945_20090414t235944_* ### april 14
so need take 15 lines snipped off april 14 example above , paste end of april 13 example.
this doesn't have part of original code, in fact best if weren't. hoping able me going.
thanks in advance! if there have been unclear , needs further explanation please let me know.
"i need strip first 15 lines off 1 file , paste them end of previous file in sequence."
if understand want correctly, can done 1 line of code:
awk 'nr==1 || fnr==16{close(f); f=filename ".new"} {print>f}' file1 file2 file3
when has run, files file1.new
, file2.new
, , file3.new
in new form lines transferred. of course, not limited 3 files: may specify many on command line.
example
to keep our example short, let's strip first 2 lines instead of 15. consider these test files:
$ cat file1 1 2 3 $ cat file2 4 5 6 7 8 $ cat file3 9 10 11 12 13 14 15
here result of running our command:
$ awk 'nr==1 || fnr==3{close(f); f=filename ".new"} {print>f}' file1 file2 file3 $ cat file1.new 1 2 3 4 5 $ cat file2.new 6 7 8 9 10 $ cat file3.new 11 12 13 14 15
as can see, first 2 lines of each file have been transferred preceding file.
how works
awk implicitly reads each file line-by-line. job of our code choose new file line should written based on line number. variable f
contain name of file writing to.
nr==1 || fnr==16{f=filename ".new"}
when reading first line of first file,
nr==1
, or when reading 16th line of whatever file on,fnr==16
, updatef
name of current file.new
added end.for short example, transferred 2 lines instead of 15, used same code
fnr==16
replacedfnr==3
.print>f
this prints current line file
f
.(if shell script, use
>>
. not shell script. awk.)
using glob specify file names
destination='media/user/directory/' awk 'nr==1 || fnr==16{close(f); f=filename ".new"} {print>f}' "$destination"*.ascii
Comments
Post a Comment