dtdt
April 22, 2014, 1:45pm
1
Task: Find keywords in each line, and append at the end of line; if not found in the line, do nothing.
the code is wrong. how to make it work. thanks a lot.
cat keywords.txt | while read line; do
awk -F"|" '{if (/$line/) {print $0"$line , ";} else print;}' outfile.txt > tmp
mv -f tmp outfile.txt
done
outfile.txt content look like this:
title1|content1|keywords... if keywords found in title1 or content1
keywords.txt content look like this: one keyword each line.
keyword1
keyword2
keyword3
Please provide sample fileS and the desired output (using code tags).
dtdt
April 22, 2014, 2:40pm
3
anything not clear buddy?
just a guess, pal!
awk -F'|' 'FNR==NR{keys[$0];next} {for(i in keys) {if ($1==i) $0=$0 OFS i; if ($2==i) $0=$0 ((NF>2)? " ":OFS) i}}1' OFS='|' keywords.txt outfile.txt
dtdt:
no good
ok, thanks for sharing and for being thorough!
Give us sample data in outfile.txt and expected output, tmp
Please provide few rows in each file
dtdt
April 23, 2014, 10:09am
8
only two files, tmp is not necessary
keywords.txt
world
we
are
loop it
outfile.txt
we damn world|this is it, this is it. loop it.|
let's play|how about that?|
you are good, just loop it.|Please provide few rows in each file|
expected output
we damn world|this is it, this is it. loop it.|world, we, loop it,
let's play|how about that?|
you are good, just loop it.|Please provide few rows in each file|are, loop it,
awk 'NR == FNR{a[NR] = tolower($0); n = NR; next}
{out = $0;
gsub("[[:punct:]]", " ", $0);
tolower($0);
for(i = 1; i <= n; i++)
{if($0 ~ a) out = (out a ",")};
print out}' keywords.txt outfile.txt
awk -F'|' 'FNR==NR{keys[$0];next} {for(i in keys) {if ($1~i) $0=$0 ((NF>3)? ",":OFS) i;if ($2~i) $0=$0 ((NF>3)? ",":OFS) i}}1' OFS='|' keywords.txt outfile.txt