How to use awk to prepend as string AND append the substring found after the last occurance of "/" for each line in a file

huangapple go评论45阅读模式
英文:

How to use awk to prepend as string AND append the substring found after the last occurance of "/" for each line in a file

问题

我有一个名为myfile.sh的文件,内容如下:

tests/test1
tests/test2
tests/test3


我想要对这个文件执行两项操作:

1. 在每一行前面添加字符串"abc "
2. 在每一行后面添加该行中最后一个"/"后面的内容

因此,我的新文件应该如下所示:

abc tests/test1 test1
abc tests/test2 test2
abc tests/test3 test3


到目前为止,我已经能够完成上述操作的第一步,并且我能够找到每一行中最后一个"/"后面的字符串。我在Windows上使用GitBash执行这些操作,因此我使用双引号而不是单引号:

awk "{print "abc " $0}" myfile.txt > mynewfile.txt


这会在每一行前面添加字符串"abc "。

awk -F/ "{print $NF}" myfile.txt > mynewfile.txt


这会找到每一行中最后一个"/"后面的字符串。

但是,我不知道如何执行第二步以获得所需的文件。
英文:

I have a file myfile.sh like this:

tests/test1
tests/test2
tests/test3

I would like to do 2 things with this file:

  1. prepend each line with "abc " string
  2. append each line with whatever is found after the last occurrence of "/" on that line

So, my new file would need to look like:

abc tests/test1 test1
abc tests/test2 test2
abc tests/test3 test3

So far I am able to do #1 above and I am able to find the strings after the last occurrence of "/" on each line. I am doing this on Windows using GitBash, hence I use double quotes instead of single quotes:

awk "{print \"abc \" $0}" myfile.txt> mynewfile.txt

,this prepends each line with string "abc ".

awk -F/ "{print $NF}" myfile.txt> mynewfile.txt

, this finds string after the last occurrence of "/" on each line.

So, that is all good but I don't know how to do #2 to get to the desired file

答案1

得分: 3

你可以使用以下awk命令来将静态字符串打印为第一列,并使用/作为FS来打印最后一列:

awk -F/ 'NF {print "abc", $0, $NF}' file

abc tests/test1 test1
abc tests/test2 test2
abc tests/test3 test3

我已将NF用作条件(仅对非空行执行操作)。


OP 在Windows上必须在awk命令周围使用",所以这里是:

# 适用于Windows / DOS系统
awk -F/ ""NF {print \"abc\", $0, $NF}"" file

# 适用于基于Unix的系统
awk -F/ ""NF {print \"abc\", \$0, $NF}"" file

如Ed建议的,最好将awk脚本保存在文件中,然后将其用作awk脚本,以避免引号转义:

cat script.awk
NF {print "abc", $0, $NF}

awk -F/ -f script.awk file

或者,以下是执行相同操作的sed命令:

sed -E ""s~.*/(.*)~abc & \~"" file
英文:

You may use this awk command to print a static string as first column and using / as FS print last column:

awk -F/ 'NF {print "abc", $0, $NF}' file

abc tests/test1 test1
abc tests/test2 test2
abc tests/test3 test3

I have used NF as condition (to do it only for non-empty lines).


OP has to use " around awk command on Windows, so here it is:

# for Windows / DOS systems
awk -F/ "NF {print \"abc\", $0, $NF}" file

# for Unix based system
awk -F/ "NF {print \"abc\", $0, $NF}" file

As suggested by Ed, better to save awk script in a file and use that as awk script to avoid quotes escaping:

cat script.awk
NF {print "abc", $0, $NF}

awk -F/ -f script.awk file

Alternatively here is a sed command to do the same:

sed -E "s~.*/(.*)~abc & \\1~" file

huangapple
  • 本文由 发表于 2023年7月14日 03:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定