我需要将一个特定的字符串移到行尾,但我完全不知道如何。这是一个例子:
:88:Some text/Some text/Some text/**TEXT TO MOVE**/Some textSome text/Some text/Some textSome text/
:29:Some textSome text/**TEXT TO MOVE**/Some textSome text/Some text/Some text/
...
每条线的长度和"有些文字"斜线之间的每一行都不同。 每行以" /"结尾。始终以":xx:"开始其中x - 一些数字。
有没有人可以帮助我?
答案 0 :(得分:0)
假设您**TEXT TO MOVE**
的字段数与相同,您可以这样做:
$ echo -e ':88:Some text/Some text/Some text/**TEXT TO MOVE**/Some textSome text/Some text/Some textSome text/
:29:Some textSome text/**TEXT TO MOVE**/Some textSome text/Some text/Some text/' \
| awk -F'/' '
{
for (i = 1; i <= NF - 5; i++) {
printf("%s/", $i)
}
printf("%s/%s/%s/", $(NF - 3), $(NF - 2), $(NF - 1));
print $(NF - 4) "/"
}'
:88:Some text/Some text/Some text/Some textSome text/Some text/Some textSome text/**TEXT TO MOVE**/
:29:Some textSome text/Some textSome text/Some text/Some text/**TEXT TO MOVE**/
答案 1 :(得分:0)
无论在移动的字符串中包含哪些RE元字符,这都会有效,因为它适用于字符串,而不是RE:
$ cat file
Some text/Some text/**TEXT TO MOVE**/Some text/Some text/
Some text/**TEXT TO MOVE**/Some textSome text/
$ awk -v str='**TEXT TO MOVE**' 's=index($0,str){$0 = substr($0,1,s-1) substr($0,s+length(str)) str }1' file
Some text/Some text//Some text/Some text/**TEXT TO MOVE**
Some text//Some textSome text/**TEXT TO MOVE**