我是新手,我真的很感激任何帮助。我需要编写一段Unix代码来搜索文件X中的字符串,然后在文件目标中进行搜索,删除之前找到的行……
我已经做了几次类似下面的尝试,但都不起作用。
grep -v "Invalid key: " issues.txt > translate1.stf ; mv translate1.stf issues.txt致以最好的问候,EB
发布于 2020-12-02 22:44:30
如果我理解您的请求,您希望删除fileX中字符串介于"Invalid Key:“和下一个句点('.')之间的任何行。包含在fileY中。
这可以在awk中完成。
如果fileX名为"input.txt“并包含
Line 1
Invalid Key: Key1. this line shows
Line 2
Invalid Key: Key2. this line is filtered
Invalid Key: Key3. this line is filtered
Invalid Key: Key4. this line shows
Line 3fileY被命名为"keysToFilter.txt“
# lines containing these keys are deleted
Key2
Key3和文件"filter“中的一个程序
#! /usr/bin/awk -f
BEGIN {
while (getline < "keysToFilter.txt") {
keys[$0] = 1
}
}
# lines matching 'Invalid Key' get this treatment
/^Invalid Key: / {
keyStart = length("Invalid Key: ") + 1
keyEnd = index($0, ".")
key = substr($0, keyStart, keyEnd - keyStart)
if ( !(key in keys) ) {
print $0
}
next
}
# every other line is unfiltered
{ print }..。然后,下面的代码将向stdout发送一个经过过滤的fileX版本
$ cat input.txt | ./filterhttps://stackoverflow.com/questions/65093242
复制相似问题