Concatenate multiple files but include filename as section headers

前端 未结 20 1615
粉色の甜心
粉色の甜心 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:15

    If you want the result in the same format as your desired output you can try:

    for file in `ls file{1..3}.txt`; \
    do echo $file | cut -d '.' -f 1; \ 
    cat $file  ; done;
    

    Result:

    file1
    bluemoongoodbeer
    file2
    awesomepossum
    file3
    hownowbrowncow
    

    You can put echo -e before and after the cut so you have the spacing between the lines as well:

    $ for file in `ls file{1..3}.txt`; do echo $file | cut -d '.' -f 1; echo -e; cat $file; echo -e  ; done;
    

    Result:

    file1
    
    bluemoongoodbeer
    
    file2
    
    awesomepossum
    
    file3
    
    hownowbrowncow
    
    0 讨论(0)
  • 2020-12-12 09:16

    I like this option

    for x in $(ls ./*.php); do echo $x; cat $x | grep -i 'menuItem'; done
    

    Output looks like this:

    ./debug-things.php
    ./Facebook.Pixel.Code.php
    ./footer.trusted.seller.items.php
    ./GoogleAnalytics.php
    ./JivositeCode.php
    ./Live-Messenger.php
    ./mPopex.php
    ./NOTIFICATIONS-box.php
    ./reviewPopUp_Frame.php
                $('#top-nav-scroller-pos-<?=$active**MenuItem**;?>').addClass('active');
                gotTo**MenuItem**();
    ./Reviews-Frames-PopUps.php
    ./social.media.login.btns.php
    ./social-side-bar.php
    ./staticWalletsAlerst.php
    ./tmp-fix.php
    ./top-nav-scroller.php
    $active**MenuItem** = '0';
            $active**MenuItem** = '1';
            $active**MenuItem** = '2';
            $active**MenuItem** = '3';
    ./Waiting-Overlay.php
    ./Yandex.Metrika.php
    
    0 讨论(0)
  • 2020-12-12 09:17

    When there is more than one input file, the more command concatenates them and also includes each filename as a header.

    To concatenate to a file:

    more *.txt > out.txt
    

    To concatenate to the terminal:

    more *.txt | cat
    

    Example output:

    ::::::::::::::
    file1.txt
    ::::::::::::::
    This is
    my first file.
    ::::::::::::::
    file2.txt
    ::::::::::::::
    And this is my
    second file.
    
    0 讨论(0)
  • 2020-12-12 09:17

    This is how I normally handle formatting like that:

    for i in *; do echo "$i"; echo ; cat "$i"; echo ; done ;
    

    I generally pipe the cat into a grep for specific information.

    0 讨论(0)
  • 2020-12-12 09:17

    you can use this simple command instead of using a for loop,

    ls -ltr | awk '{print $9}' | xargs head
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题