Concatenate multiple files but include filename as section headers

前端 未结 20 1621
粉色の甜心
粉色の甜心 2020-12-12 09:08

I would like to concatenate a number of text files into one large file in terminal. I know I can do this using the cat command. However, I would like the filename of each fi

20条回答
  •  [愿得一人]
    2020-12-12 09:22

    If you want to replace those ugly ==> <== with something else

    tail -n +1 *.txt | sed -e 's/==>/\n###/g' -e 's/<==/###/g' >> "files.txt"
    

    explanation:

    tail -n +1 *.txt - output all files in folder with header

    sed -e 's/==>/\n###/g' -e 's/<==/###/g' - replace ==> with new line + ### and <== with just ###

    >> "files.txt" - output all to a file

提交回复
热议问题