awk: add a line if missing - scripting
I've an awk script which processes .ICS calendar files.
I need to add the ATTENDEE line if it's missing.
I already have a script which parses all the events taking in considerations only the ones I need given a CHECKPARM criteria. I need to add the ATTENDEE if it's not present already.
/BEGIN:VEVENT/ { cache = 1; }
/CHECKPARM/ {
if( index( $0, var ) )
printf( "%s", cached_lines );
else
drop = 1;
cached_lines = "";
cache = 0;
}
# this doesn't work
#!~ /ATTENDEE/ {
# printf ("ATTENDEE: %s", organizer);
#}
cache {
cached_lines = cached_lines $0 "\n";
next;
};
!drop { print; }
/END:VEVENT/ { drop = 0; }
Try using a flag, if line is present, set it, if not, add line.
Something like this:
/ATTENDEE/ {att = 1}
!att {
printf ("ATTENDEE: %s\n", organizer)
}
Related
How to remake the program so that words are passed in function arguments in the KOTLIN programming language?
Need to create a function that implements the attached algorithm, to which all words are passed in the function arguments. For example: f ("dfd" dd "ddd"); My code: fun main() { var s = readLine(); var w = Array(128){0} //To mark characters from a word 1 var g = Array(128){0}//When we encounter a space, we add units from the first array to the corresponding elements of the second, zeroing them in the first. if(s!=null) { for(c in s) { if(c.toInt() > 127 || c.toInt()<0) { println("Input error, try again"); return; } //Checking for space. if(c.toInt() != 32) w[c.toInt()] = 1; else for(k in 0..127) { if(w[k] == 1) { g[k] += 1; w[k] = 0; } } } //For the last word, if there was no space after it. for(k in 0..127) { if(w[k] == 1) { g[k] += 1; w[k] = 0; } } } //Displaying matched characters to the screen for(k in 0..127) { if(g[k]>1) { println(k.toChar()); } } } This program searches for characters that match at least two words in a string Example input: hello world output: lo
There's already utilities for these in Kotlin, I highly recommend you to read the docs before asking these type of questions. The groupingBy should do what you want: readLine()?.let { input -> input.groupingBy { it }.eachCount() .forEach { if (it.value > 1 && it.key != ' ') println(it.key) } }
AWK - Working with two files
I have these two csv files: File A: veículo;carro;sust automóvel;carro;sust viatura;carro;sust breve;rápido;adj excepcional;excelente;adj maravilhoso;excelente;adj amistoso;simpático;adj amigável;simpático;adj ... File B: "A001","carro","sust","excelente","adj","ocorrer","adv","bom","adj" ... In the file A, $1(word) is synonym for $2(word) and $3(word) the part of speech. In the lines of the file B we can skip $1,the remaining columns are words and their part of speech. What I need to to do is to look line by line each pair (word-pos) in the file A and generate a line for each synonym. It is difficult to explain. Desired Output: "A001","carro","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","viatura","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","veículo","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","automóvel","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","carro","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","viatura","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","veículo","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","automóvel","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","carro","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","viatura","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","veículo","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","automóvel","sust","maravilhoso","adj","ocorrer","adv","bom","adj" Done: BEGIN { FS="[,;]"; OFS=";"; } FNR==NR{ sinonim[$1","$2","$3]++; next; } { s1=split($0,AX,"\n"); for (i=1;i<=s1;i++) { s2=split(AX[i],BX,","); for (j=2;j<=NF;j+=2) { lineX=BX[j]","BX[j+1]; gsub(/\"/,"",lineX); for (item in sinonim) { s3=split(item,CX,","); lineS=CX[2]","CX[3]; if (lineX == lineS) { BX[j]=CX[1]; lineD="" for (t=1;t<=s2;t++) { lineD=lineD BX[t]","; } lineF=lineF lineD"\n"; } } } } print lineF }
$ cat tst.awk BEGIN { FS=";" } NR==FNR { synonyms[$2,$3][$2]; synonyms[$2,$3][$1]; next } FNR==1 { FS=OFS="\",\""; $0=$0 } { gsub(/^"|"$/,"") for (i=2;i<NF;i+=2) { if ( ($i,$(i+1)) in synonyms) { for (synonym in synonyms[$i,$(i+1)]) { $i = synonym for (j=2;j<NF;j+=2) { if ( ($j,$(j+1)) in synonyms) { for (synonym in synonyms[$j,$(j+1)]) { orig = $0 $j = synonym if (!seen[$0]++) { print "\"" $0 "\"" } $0 = orig } } } } } } } . $ awk -f tst.awk fileA fileB "A001","carro","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","veículo","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","automóvel","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","viatura","sust","excelente","adj","ocorrer","adv","bom","adj" "A001","carro","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","carro","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","veículo","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","veículo","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","automóvel","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","automóvel","sust","excepcional","adj","ocorrer","adv","bom","adj" "A001","viatura","sust","maravilhoso","adj","ocorrer","adv","bom","adj" "A001","viatura","sust","excepcional","adj","ocorrer","adv","bom","adj" The above uses GNU awk for multi-dimensional arrays, with other awks it's a simple tweak to use synonyms[$2,$3] = synonyms[$2,$3] " " $2 etc. or similar and then split() later instead of synonyms[$2,$3][$2] and in.
BEGIN { FS="[,;]"; OFS="," } NR == FNR { key = "\"" $2 "\""; synonym[key] = synonym[key] "," $1; next } { print; if ($2 in synonym) { count = split(substr(synonym[$2], 2), choices) for (i = 1; i <= count; i++) { $2 = "\"" choices[i] "\"" print } } }
Awk input variable as a rule
Good day! I have the next code: BLOCK=`awk ' /\/\* R \*\// { level=1 count=0 } level { n = split($0, c, ""); for (i = 1; i <= n; i++) { printf(c[i]); if (c[i] == ";") { if(level==1) { level = 0; if (count != 0) printf("\n"); }; } else if (c[i] == "{") { level++; count++; } else if (c[i] == "}") { level--; count++; } } printf("\n") }' $i` That code cuts the piece of the file from /* R */ mark to the ';' symbol with taking into account the details like braces etc. But that isn't important. I want to replace the hard-coded /* R */ by the variable: RECORDSEQ="/* R */" ... BLOCK=`awk -v rec="$RECORDSEQ" ' rec { level=1 count=0 } But that doesn't work. How can I fix it? Thank you in advance.
Found the solution: RECORDSEQ="/* R */" # Construct regexp for awk RECORDSEQREG=`echo "$RECORDSEQ" | sed 's:\/:\\\/:g;s:\*:\\\*:g'` # Cycle for files for i in $SOURCE; do # Find RECORDSEQ and cut out the block BLOCK=`awk -v rec="$RECORDSEQREG" ' $0 ~ rec { level=1 count=0 } ... Many thanks to people who helped.
awk nesting curling brackets
I have the following awk script where I seem to need to next curly brackets. But this is not allowed in awk. How can I fix this issue in my script here? The problem is in the if(inqueued == 1). BEGIN { print "Log File Analysis Sequencing for " + FILENAME; inqueued=0; connidtext=""; thisdntext=""; } /message EventQueued/ { inqueued=1; print $0; } if(inqueued == 1) { /AttributeConnID/ { connidtext = $0; } /AttributeThisDN / { thisdntext = $2; } #space removes DNRole } #if first chars are a timetamp we know we are out of queued text /\#?[0-9]+:[0-9}+:[0-9]+/ { if(thisdntext != 0) { print connidtext; print thisdntext; } inqueued = 0; connidtext=""; thisdntext=""; }
try to change if(inqueued == 1) { /AttributeConnID/ { connidtext = $0; } /AttributeThisDN / { thisdntext = $2; } #space removes DNRole } to inqueued == 1 { if($0~ /AttributeConnID/) { connidtext = $0; } if($0~/AttributeThisDN /) { thisdntext = $2; } #space removes DNRole } or inqueued == 1 && /AttributeConnID/{connidtext = $0;} inqueued == 1 && /AttributeThisDN /{ thisdntext = $2; } #space removes DNRole
awk is made up of <condition> { <action> } segments. Within an <action> you can specify conditions just like you do in C with if or while constructs. You have a few other problems too, just re-write your script as: BEGIN { print "Log File Analysis Sequencing for", FILENAME } /message EventQueued/ { inqueued=1 print } inqueued == 1 { if (/AttributeConnID/) { connidtext = $0 } if (/AttributeThisDN/) { thisdntext = $2 } #space removes DNRole } #if first chars are a timetamp we know we are out of queued text /\#?[0-9]+:[0-9}+:[0-9]+/ { if (thisdntext != 0) { print connidtext print thisdntext } inqueued=connidtext=thisdntext="" } I don't know if that'll do what you want or not, but it's syntactically correct at least.
Ignoring escaped delimiters (commas) with awk?
If I had a string with escaped commas like so: a,b,{c\,d\,e},f,g How might I use awk to parse that into the following items? a b {c\,d\,e} f g
{ split($0, a, /,/) j=1 for(i=1; i<=length(a); ++i) { if(match(b[j], /\\$/)) { b[j]=b[j] "," a[i] } else { b[++j] = a[i] } } for(k=2; k<=length(b); ++k) { print b[k] } } Split into array a, using ',' as delimiter Build array b from a, merging lines that end in '\' Print array b (Note: Starts at 2 since first item is blank) This solution presumes (for now) that ',' is the only character that is ever escaped with '\'--that is, there is no need to handle any \\ in the input, nor weird combinations such as \\\,\\,\\\\,,\,.
{ gsub("\\\\,", "!Q!") n = split($0, a, ",") for (i = 1; i <= n; ++i) { gsub("!Q!", "\\,", a[i]) print a[i] } }
I don't think awk has any built-in support for something like this. Here's a solution that's not nearly as short as DigitalRoss's, but should have no danger of ever accidentally hitting your made-up string (!Q!). Since it tests with an if, you could also extend it to be careful about whether you actually have \\, at the end of your string, which should be an escaped slash, not comma. BEGIN { FS = "," } { curfield=1 for (i=1; i<=NF; i++) { if (substr($i,length($i)) == "\\") { fields[curfield] = fields[curfield] substr($i,1,length($i)-1) FS } else { fields[curfield] = fields[curfield] $i curfield++ } } nf = curfield - 1 for (i=1; i<=nf; i++) { printf("%d: %s ",i,fields[i]) } printf("\n") }