Today I was looking for a way to combine a set of files in one but before appending a given file, the 2 first lines as well as the last line had to be removed. On Stackoverflow I found a couple of answers (see questions 18006581, 12176492 and 5410757) and based on these I decided to use a combination of head and tail. I ended up creating the following shell script:
|
|
Let’s break it down:
- first the Shebang to mention that it is a bash script;
- then
touch biblio.yaml
creates the file where all other files will be appended; - a
for
loop over the set of files inrefs
that ends by.yml
, at each iteration of the loop,$f
will be a new file; - for each file, print the last lines of the files starting with line 3 (
tail -n+3
), then take the result (|
) and print the first lines of it but stop at the line before the last one (head -n-1
) then append the result in biblio.yaml (>> biblio.yaml
); - the iteration with the addition of a line break:
echo "\n" >> biblio.yaml
.