Reputation: 37
I'm struggling to fetch data from a file having the same format as mentioned below into a new output file(CSV) using shell Script.
[+++] Added Names: [+++]
-> Add- abc.txt (732):
-> Add- bcd.txt (490):
[+++] Enabled Names: [+++]
-> Mod- cde.txt (105):
-> Mod- efg.txt (105):
[+++] Deleted Names: [+++]
-> Del- fgh.txt (4):
-> Del- xyz.txt (45):
[+++] Added non-names lines: [+++]
-> Added to test.txt (41):
-> Added to bgh.txt (41):
# This distribution may contain names under different licenses.
and the expected output should look like
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
Here Output renames the same as per last.The only thing which is not required is -> Added to test.txt (41): and -> Added to bgh.txt (41):.which is part of [+++] Added non-names lines: [+++]
Upvotes: 1
Views: 50
Reputation: 785176
You can use this awk
command with a custom input field separator:
awk -v OFS=, -F '[->:()[:blank:]]+' 'index($0, "[+++]") {
p=($0 ~ / (Added|Deleted|Enabled) Names:/); next} p && /-> /{print $2, $3, $4}' file
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
Upvotes: 3
Reputation: 92854
Another GNU awk
solution:
awk -v FPAT='[[:alnum:].]+' '/->/{ print $1,$2,$3 }' OFS=',' file
The output:
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
Upvotes: 1
Reputation: 445
Another approach using gsub
function:-
awk '/->/{gsub(/[->():]/,X);$1=$1;print}' OFS=, file
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
Upvotes: 1