我希望将这些行提取为与Pattern匹配的csv文件,然后将相同的Pattern附加到每个提取行的末尾,作为csv文件的新添加列。
FILE.CSV
file.csv
/var/log/0,33,New file,0
/var/log/0,34,Size increased,2345
/abc/Repli,11,New file,0
/abc/Repli,87,Size Increase,11
In above file file.csv, I executed
sed -n -i"" '/Repli/ s/$/,Repli/p' file.csv
这会删除我不想要的剩余行。
答案 0 :(得分:0)
要仅选择包含pattern的行,然后在该行的末尾添加pattern作为新列:
awk '/pattern/ {print $0 ",pattern"}' file.csv >tmp$$ && mv tmp$$ file.csv
或者,
sed -b -n -i"" '/pattern/ s/$/,pattern/p' file.csv
awk '/pattern/ {$0=$0 ",pattern"} 1'
或者,
sed -b -i"" '/pattern/ s/$/,pattern/' file.csv
sed -i"" 's/\r//; /pattern/ s/$/,pattern/' file.csv
假设模式包含/var/log/abc/file/0/
之类的斜杠。然后:
sed -i"" 's/\r//; \|pattern| s|$|,pattern|' file.csv
例如:
sed -i"" 's/\r//; \|/var/log/abc/file/0/| s|$|,/var/log/abc/file/0/|' file.csv
答案 1 :(得分:0)
I found a solution to match paths using sed. I did it through escap character and it worked.
Pattern="\/var\/log\/Model\/1\/"
Module=BE
sudo sed -i"" "s/\r//; /$Pattern/ s/$/,$Module/" resultFile.csv
工作得很好!!
答案 2 :(得分:0)
如果找到匹配项,则会在代码段下方附加该模式。如果找不到匹配项,则只打印该行,
awk '{if($0 ~ /pattern/) print $0",pattern"; else print $0;}' file.csv