Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 重新创建tail-n到文本文件的输出_Regex_Bash_Shell_Unix - Fatal编程技术网

Regex 重新创建tail-n到文本文件的输出

Regex 重新创建tail-n到文本文件的输出,regex,bash,shell,unix,Regex,Bash,Shell,Unix,我在一个目录中有一堆bash脚本,我通过$tail-n+1--*.sh“备份”了这些脚本 该尾部的输出类似于: ==> do_stuff.sh <== #! /bin/bash cd ~/my_dir source ~/my_dir/bin/activate python scripts/do_stuff.py ==> do_more_stuff.sh <== #! /bin/bash cd ~/my_dir python scripts/do_more_stuff.

我在一个目录中有一堆bash脚本,我通过
$tail-n+1--*.sh“备份”了这些脚本

该尾部的输出类似于:

==> do_stuff.sh <==
#! /bin/bash
cd ~/my_dir
source ~/my_dir/bin/activate
python scripts/do_stuff.py


==> do_more_stuff.sh <==
#! /bin/bash
cd ~/my_dir
python scripts/do_more_stuff.py
==>dou_stuff.sh dou more_stuff.sh


我开始尝试找到一个匹配的正则表达式,它可能看起来像这样
(==>.\.sh.\.sh.\.sh假设您的备份文件名为backup.txt

perl -ne "if (/==> (\S+) <==/){open OUT,'>',$1;next}print OUT $_" backup.txt
perl-ne“if(/==>(\S+),$1;next}打印出$\uu'backup.txt
#!/bin/bash
当读取-r行时;执行
如果[[$line=~^==\>[[:space:]](.*)[[:space:]\>“$out”
完成

缺点:除最后一个文件外,每个创建的文件末尾都有额外的空行。

请修复引号。Bash替换
$1
perl -ne 'if (/==> (\S+) <==/){open OUT,">",$1;next}print OUT $_' backup.txt
#!/bin/bash

while read -r line; do
  if [[ $line =~ ^==\>[[:space:]](.*)[[:space:]]\<==$ ]]; then
    out="${BASH_REMATCH[1]}"
    continue
  fi
  printf "%s\n" "$line" >> "$out"
done < backup.txt