Date: Sun, 10 Sep 2017 21:00:15 +0200 From: Polytropon <freebsd@edvax.de> To: Ernie Luzar <luzar722@gmail.com> Cc: "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org> Subject: Re: awk help Message-ID: <20170910210015.eaee44cd.freebsd@edvax.de> In-Reply-To: <59B53ED2.3000409@gmail.com> References: <59B53ED2.3000409@gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Allow me a few comments regarding sh -> awk for reformatting your input data. On Sun, 10 Sep 2017 09:32:02 -0400, Ernie Luzar wrote: > # Locate and replace carriage return with blank. > line=`echo -n "${line}" | tr '\r' ' '` Drop the ^Ms in a pipe step ... | tr -d '\r' | ... > # Locate and replace tab with blank. > line=`echo -n "${line}" | tr '\t' ' '` No need, awk defaults to tab(s) and/or space(s) as field separators, and you can easily access the fields with $1, $2, $3 and so on. > # Drop blank lines. > blank_line=`echo -n $line | cut -c 1-1` > if [ "$blank_line" = " " ]; then > continue > fi Just add a rule (length > 0) infront of your { ... awk statements for each line }. > # Drop lines with localhost in it. > localhost=`echo -n $line | cut -w -f 2` > if [ "$localhost" = "localhost" ]; then > continue > fi Expand the rule like (length > 0 && $2 != "localhost") { ... }, in case "localhost" is the exact text; if you want to use a reges, use $2 != /localhost/ instead. > # Drop line with # in cloumn 1 as a comment. > comment1=`echo -n $line | cut -c 1-1` > if [ "$comment1" = "#" ]; then > continue > fi Add another rule as reges !/^#/ && ( ... as above ... ) { ... } to filter those. Or, also possible, use ... | grep -v "^#" | ... infront of awk. > # Drop line with word Malvertising starting in cloumn 1 > comment1=`echo -n $line | cut -w -f 1` > if [ "$comment1" = "Malvertising" ]; then > continue > fi See above. > # Out put record. > ip=`echo -n $line | cut -w -f 1` > $trace_on echo "ip = ${ip}" > if [ "$ip" = "127.0.0.1" -o "$ip" = "0.0.0.0" ]; then > domain_name=`echo -n $line | cut -w -f 2` > echo "local-zone: \"${domain_name}\" always_nxdomain" >> $host_out > continue > else > domain_name=`echo -n $line | cut -w -f 1` > echo "local-zone: \"${domain_name}\" always_nxdomain" >> $host_out > fi Construct an output statement as desired. Use variables instead of $1, $2, $3 if the whole things gets too complex, for example like this (not tested, just for illustration): #!/bin/sh cat input.txt | tr -d '\r' | awk ' !/^#/ && (length > 0) { ip = $1 host = $2 if (!(host == "localhost" || ip == "127.0.0.1" || ip == "0.0.0.0")) printf("local-zone: \"%s\" always_nxdomain\n", ip) else printf("local-zone: \"%s\" always_nxdomain\n", host) }' > output.txt That should be basically what you need. You now just have to combine the moving parts correctly. :-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20170910210015.eaee44cd.freebsd>