将多个文本文件合并为一个文本文件

问题描述 投票:0回答:1

文件夹有几个文本文件,我想在添加新文件或编辑文件时将它们合并到

Merge.txt
。没有任何进展,只需复制文本并提交。

这应该非常简单,但我找不到任何人有解决方案。

# 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
github-actions
1个回答
0
投票

您可以使用类似的方法将多个 .txt 文件合并到单个 Merge.txt 文件,如果您想提交该 Merge.txt 文件,则可以使用其他现有操作。我将仅展示文件合并示例。

name: Merge multiple text file to 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

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.