skip to Main Content

Folder has several text files and I want to merge them to Merge.txt when adding new files or editing files. Nothing advanced only copy the text and commit.

This should be super easy, but I can’t find anyone who has a solution.

# This is a basic workflow to help you get started with Actions

name: CI

Controls when the workflow will run

on:

Triggers the workflow on push or pull request events but only for the "main" branch

push: branches: [ "main" ] 
pull_request: branches: [ "main" ]

Allows you to run this workflow manually from the Actions tab

workflow_dispatch:

A workflow run is made up of one or more jobs that can run sequentially or in parallel

jobs:

This workflow contains a single job called "build"

build: # The type of runner that the job will run on runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
  # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  - uses: actions/checkout@v4

  # Runs a single command using the runners shell
  - name: Merge TXT Files
    run: |
       find . -type f -name '*.txt' | while read fname; do

2

Answers


  1. Chosen as BEST ANSWER
    # This is a basic workflow to help you get started with Actions
    
    name: Merge multiple text files into a single file
    
    # Controls when the workflow will run
    on:
      # Triggers the workflow on push or pull request events but only for the "main" branch
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
      # Allows you to run this workflow manually from the Actions tab
      workflow_dispatch:
    
    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
      # This workflow contains a single job called "build"
      build:
        # The type of runner that the job will run on
        runs-on: ubuntu-latest
    
        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
          # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
          - uses: actions/checkout@master
            with:
              persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
              fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.
            
          - name: Merge TXT Files in root folder
            run: |
              # Create and show list of files being processed
              echo "Files being processed:"
              find . -type f -name '*.txt' ! -name 'Merge.txt' -ls
              
              # Merge files and create Merge.txt
              find . -type f -name '*.txt' ! -name 'Merge.txt' -exec cat {} + > Merge.txt
              
              # Show the merged file was created
              echo -e "nMerge.txt has been created:"
              ls -l Merge.txt
              
              # Show the content of Merge.txt
              echo -e "nContent of Merge.txt:"
              cat Merge.txt
    
          - name: Commit files
            run: |
             git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
             git config --local user.name "github-actions[bot]"
             git add Merge.txt
             git commit -a -m "Add changes"
    
          - name: Push changes
            uses: ad-m/github-push-action@master
            with: 
              github_token: ${{ secrets.GITHUB_TOKEN }}
              branch: ${{ github.ref }}
    

  2. You can use something like this to merge multiple .txt files to a single Merge.txt file, if you want to commit that Merge.txt file then you can use other existing actions. I am going to show only the file merging example.

    name: Merge multiple text files into a single file
    
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v4
    
          - name: Merge TXT Files
            run: |
              # Create and show list of files being processed
              echo "Files being processed:"
              find . -type f -name '*.txt' ! -name 'Merge.txt' -ls
              
              # Merge files and create Merge.txt
              find . -type f -name '*.txt' ! -name 'Merge.txt' -exec cat {} + > Merge.txt
              
              # Show the merged file was created
              echo -e "nMerge.txt has been created:"
              ls -l Merge.txt
              
              # Show the content of Merge.txt
              echo -e "nContent of Merge.txt:"
              cat Merge.txt
    

    Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search