上一级提取文件夹名称并使用sed插入文本文件

时间:2020-07-20 08:09:59

标签: bash ubuntu sed

我在几个User(n)文件夹下有几个文件。

示例:

用户1文件夹目录结构:/home/Ubuntu/Group1/Team1/User1/01-01-2020/

100510_destination1.ext
100520_destination1.ext
..etc

用户2文件夹目录结构:/home/Ubuntu/Group1/Team1/User2/01-01-2020/

103030_sample3.ext
113020_sample3.ext
..etc

用户(n)文件夹。./home/Ubuntu/Group1/Team1/User(n)/01-01-2020/

112818_any.ext
112918_any.ext
..etc

我想将所有用户(n)文件中的重要信息合并到一个文本文件中。我要创建一个新的日期文件夹,而不是“用户”文件夹,所有用户信息都在一个文本文件中。

其外观如下所示:

输出文本日志文件目录结构:/home/Ubuntu/Group1/01-01-2020/

10:05:10 Who: User1, To: destination1

10:30:30 Who: User2, To: sample3

11:28:18 Who: User(n), To: any

我使用printf命令将输出文件创建为文本。

printf '%s\n' * > output.txt

100510_destination1.ext

103030_sample3.ext

112818_any.ext

然后运行脚本

sed 's/./&:/2;s/./&:/5;s/./& To.:/8;s/.//13;s/....$//' output.txt

我能够得到:

10:05:10 To.:destination1

10:30:30 To.:sample3

11:28:18 To.:any

但是不知道如何获取文件夹名称。如何提取User(n)名称(即上一级的文件夹名称)并将其插入文本?可以使用sed将其添加到同一命令中吗?或更像bash脚本?

2 个答案:

答案 0 :(得分:0)

假设您已将完整的文件夹路径/Home/Ubuntu/Group1/Team1/User2/01-01-2020存储在bash变量folderpath中。您可以通过以下方式获取User2文件夹名称

user_foldername=$(basename "$(dirname "$folderbase")")

答案 1 :(得分:0)

  • 首先列出您感兴趣的文件
  • 然后将它们与正则表达式匹配
  • 并用适当的替换物代替

以下内容:

stages:
  - setup
  #- test
  - beta
  - production
  - tag
  - appstore

variables:
  LC_ALL: 'en_US.UTF-8'
  LANG: 'en_US.UTF-8'
  BUNDLE_GEMFILE: "Gemfile_fix"

# Setup Lanes

Node:
  stage: setup
  artifacts:
    paths: 
      - ./node_modules
    expire_in: 6h
  script: yarn install
  tags: 
    - reactnative

# Test Lanes 

# iOS Test:
#   stage: beta
#   script:
#     - bundle config set path './tmp-fastlane-ci-bundle'
#     - react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios
#     - bundle exec fastlane ios beta --env test
#   tags: 
#     - reactnative
#   only: 
#     - develop
#     - /^feature/    

Android Test:
  stage: beta
  script:
    - echo "$BUNDLE_GEMFILE"    
    - bundle config set path './tmp-fastlane-ci-bundle'    
    - bundle exec npx jetifier
    - react-native bundle --platform android --dev true --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
    - export ANDROID_HOME=/usr/local/share/android-sdk 
    - export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
    - echo $KEYSTORE_FILE | base64 -D > debug.keystore
    - export KEY_LOCATION=$(PWD)/debug.keystore
    - export KEYSTORE_PASS=$KEYSTORE_PASSWORD
    - export KEY_ALIAS=$KEY_ALIAS
    - export KEY_PASS=$KEY_PASSWORD
    - bundle exec fastlane android beta --env test
  tags: 
    - reactnative
  only: 
    - develop
    - /^feature/


 

输出:

find ./home/Ubuntu/Group1/Team1/ -type f -regex './home/Ubuntu/Group1/Team1/.*/01-01-2020/.*.ext' |
sed 's@.*/\(User[0-9]*\)/[^/]*/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)_\([^\.]*\).ext$@\2:\3:\4 Who: \1, To: \5@'

tested on repl。您还可以根据自己的喜好添加10:05:10 Who: User1, To: destination1 10:05:20 Who: User1, To: destination1 10:30:30 Who: User2, To: sample3 11:30:20 Who: User2, To: sample3 11:28:18 Who: User3, To: any 11:29:18 Who: User3, To: any 或重写-maxdepth 7 -mindepth 7

如何获取文件夹名称。

使用-regex对于regex给您带来的灵活性似乎有些la脚。正则表达式不是用于替换第n个字符,而是用于匹配输入。只需从后或从前计数次数匹配s/./soemthing/5,即可获取文件夹名称。因此/可以从背面将第3个文件夹保存在^./[^/]*/[^/]*/\([^/]*\)/[^/]*/[^/]*$中的反向引用中。注意,在替代命令中,任何字符都可以用作分隔符,即。 sedsed 's/a/b/'sed 's~a~b~相同。

如何提取User(n)名称(即上一级的文件夹名称)并将其插入文本?

用正则表达式匹配,用反向引用替换。

可以使用sed将其添加到同一命令中吗?或更像bash脚本?

只要您一次需要解析一行输入,sed 's#a#b#'通常就足够简单了。对于更复杂的内容,请尝试sed

相关问题