如何在linux bash提示符下拆分模式上的字符串并返回我的模式的最后一个实例以及之后的所有内容

时间:2022-10-20 10:46:19

This is my first question on *, I hope it's not too noob for this forum.
Thanks for your help in advance!!!

[PROBLEM]
I have a Linux bash variable in my bash script with the below content:

这是我在*上的第一个问题,我希望这个论坛不是太棒了。感谢您的帮助! [问题]我的bash脚本中有一个Linux bash变量,其内容如下:

[split]
this is a test 1
[split]
this is a test 2
[split]
this is a test 3
this is a test 4
this is a test 5

[拆分]这是一个测试1 [拆分]这是一个测试2 [拆分]这是一个测试3这是一个测试4这是一个测试5

How can I split this file on the string "[split]" and return the last section after the split?

如何在字符串“[split]”上拆分此文件,并在拆分后返回最后一部分?

this is a test 3
this is a test 4
this is a test 5

这是一个测试3这是一个测试4这是一个测试5

The last section can vary in length but it is always at the end of the "string" / "file"

最后一节的长度可能不同,但它始终位于“字符串”/“文件”的末尾

3 个解决方案

#1


0  

Using awk, set record separator to the regular expression representing the split string, print the last record at END.

使用awk,将记录分隔符设置为表示拆分字符串的正则表达式,在END处打印最后一条记录。

gawk 'BEGIN{ RS="[[]split[]]" } END{ print $0 }' tmp/test.txt

Result assuming input coming from a file:

假设输入来自文件的结果:

this is a test 3
this is a test 4
this is a test 5

#2


-1  

How about this ? :)

这个怎么样 ? :)

FILE="test.txt"
NEW_FILE="test_result.txt"
SPLIT="split"

while read line
do
if [[ $line == $SPLIT ]]
then
    $(rm ${NEW_FILE})
else
    $(echo -e "${line}" >> ${NEW_FILE})
fi

done < $FILE

#3


-1  

#!/bin/bash

s="[split]
this is a test 1
[split]
this is a test 2
[split]
this is a test 3
this is a test 4
this is a test 5"

a=()
i=0
while read -r line
do
  a[i]="${a[i]}${line}"$'\n'
  if [ "$line" == "[split]" ]
  then
     let ++i
  fi
done <<< "$s"

echo ${a[-1]}

I simply read each line from the string into an array and when I encounter [split] ,I increment the array index.At last,I echo the last element.

我只是将字符串中的每一行读入数组,当遇到[split]时,我会增加数组索引。最后,我回显最后一个元素。

EDIT: if you just want the last part no need for an array too.You could do something like

编辑:如果你只是想要最后一部分也不需要数组。你可以做类似的事情

while read -r line
do
  a+="${line}"$'\n'
  if [ "$line" == "[split]" ]
  then
     a=""
  fi
done <<< "$s"

echo $a

#1


0  

Using awk, set record separator to the regular expression representing the split string, print the last record at END.

使用awk,将记录分隔符设置为表示拆分字符串的正则表达式,在END处打印最后一条记录。

gawk 'BEGIN{ RS="[[]split[]]" } END{ print $0 }' tmp/test.txt

Result assuming input coming from a file:

假设输入来自文件的结果:

this is a test 3
this is a test 4
this is a test 5

#2


-1  

How about this ? :)

这个怎么样 ? :)

FILE="test.txt"
NEW_FILE="test_result.txt"
SPLIT="split"

while read line
do
if [[ $line == $SPLIT ]]
then
    $(rm ${NEW_FILE})
else
    $(echo -e "${line}" >> ${NEW_FILE})
fi

done < $FILE

#3


-1  

#!/bin/bash

s="[split]
this is a test 1
[split]
this is a test 2
[split]
this is a test 3
this is a test 4
this is a test 5"

a=()
i=0
while read -r line
do
  a[i]="${a[i]}${line}"$'\n'
  if [ "$line" == "[split]" ]
  then
     let ++i
  fi
done <<< "$s"

echo ${a[-1]}

I simply read each line from the string into an array and when I encounter [split] ,I increment the array index.At last,I echo the last element.

我只是将字符串中的每一行读入数组,当遇到[split]时,我会增加数组索引。最后,我回显最后一个元素。

EDIT: if you just want the last part no need for an array too.You could do something like

编辑:如果你只是想要最后一部分也不需要数组。你可以做类似的事情

while read -r line
do
  a+="${line}"$'\n'
  if [ "$line" == "[split]" ]
  then
     a=""
  fi
done <<< "$s"

echo $a