gawk

Using awk to interpolate data column based in a data file with date and time

筅森魡賤 提交于 2019-12-01 14:54:17
The following file has multiple columns with date, time and incomplete data set as shown using a simple file # Matrix.txt 13.09.2016:23:44:10;;4.0 13.09.2016:23:44:20;10.0; 13.09.2016:23:44:30;; 13.09.2016:23:44:40;30.0;7.0 How can I do an linear interpolation on each column using awk to get the missing data: # Output.txt 13.09.2016:23:44:10;0.0;4.0 13.09.2016:23:44:20;10.0;5.0 13.09.2016:23:44:30;20.0;6.0 13.09.2016:23:44:40;30.0;7.0 Here is one solution in Gnu awk. It runs twice for the first given data file, remembers first and last data points ( y 1 , y 2 ) and their timestamps ( x 2 , x 2

Simple trouble with awk and regex

微笑、不失礼 提交于 2019-12-01 09:13:37
echo xx y11y rrr | awk '{ if ($2 ~/y[1-5]{2}y/) print $3}' Why I cannot get any output? Thank you. You need to enable "interval expressions" in regular expression matching by specifying either the --posix or --re-interval option. e.g. echo xx y11y rrr | awk --re-interval '{ if ($2 ~ /y[1-5]{2}y/) print $3} From the man page: --re-interval Enable the use of interval expressions in regular expression matching (see Regular Expressions, below). Interval expressions were not traditionally available in the AWK language. The POSIX standard added them, to make awk and egrep consistent with each other.

How to skip a directory in awk?

谁说我不能喝 提交于 2019-12-01 04:47:50
Say I have the following structure of files and directories: $ tree . ├── a ├── b └── dir └── c 1 directory, 3 files That is, two files a and b together with a dir dir , where another file c stands. I want to process all the files with awk ( GNU Awk 4.1.1 , exactly), so I do something like this: $ gawk '{print FILENAME; nextfile}' * */* a b awk: cmd. line:1: warning: command line argument `dir' is a directory: skipped dir/c All is fine but the * also expands to the directory dir and awk tries to process it. So I wonder: is there any native way awk can check if the given element is a file or

Sed replace pattern with line number

喜欢而已 提交于 2019-12-01 04:01:11
I need to replace the pattern ### with the current line number. I managed to Print in the next line with both AWK and SED. sed -n "/###/{p;=;}" file prints to the next line, without the p; , it replaces the whole line. sed -e "s/###/{=;}/g" file used to make sense in my head, since the =; returns the line number of the matched pattern, but it will return me the the text {=;} What am i Missing? I know this is a silly question. I couldn't find the answer to this question in the sed manual, it's not quite clear. If possible, point me what was i missing, and what to make it work. Thank you Simple

How to skip a directory in awk?

我与影子孤独终老i 提交于 2019-12-01 03:06:20
问题 Say I have the following structure of files and directories: $ tree . ├── a ├── b └── dir └── c 1 directory, 3 files That is, two files a and b together with a dir dir , where another file c stands. I want to process all the files with awk ( GNU Awk 4.1.1 , exactly), so I do something like this: $ gawk '{print FILENAME; nextfile}' * */* a b awk: cmd. line:1: warning: command line argument `dir' is a directory: skipped dir/c All is fine but the * also expands to the directory dir and awk tries

Sed replace pattern with line number

自古美人都是妖i 提交于 2019-12-01 01:37:06
问题 I need to replace the pattern ### with the current line number. I managed to Print in the next line with both AWK and SED. sed -n "/###/{p;=;}" file prints to the next line, without the p; , it replaces the whole line. sed -e "s/###/{=;}/g" file used to make sense in my head, since the =; returns the line number of the matched pattern, but it will return me the the text {=;} What am i Missing? I know this is a silly question. I couldn't find the answer to this question in the sed manual, it's

awk: fatal: Invalid regular expression when setting multiple field separators

我的未来我决定 提交于 2019-12-01 01:17:10
I was trying to solve Grep regex to select only 10 character using awk . The question consists in a string XXXXXX[YYYYY--ZZZZZ and the OP wants to print the text in between the unique [ and -- strings within the text. If it was just one - I would say use [-[] as field separator (FS). This is setting the FS to be either - or [ : $ echo "XXXXXXX[YYYYY-ZZZZ" | awk -F[-[] '{print $2}' YYYYY The tricky point is that [ has also a special meaning as a character class, so that to make it be correctly interpreted as one of the possible FS it cannot be written in the first position. Well, this is done

multidimensional arrays in awk

ぃ、小莉子 提交于 2019-11-30 21:30:54
I tried creating a pseudo-multidimensional array in awk. # Calculate cumulative context score BEGIN { FS=OFS="\t" } { a[$2+FS+$7,$3]+=$6 } END { for (i,j) in a { print i,j,a[i,j] } } Output: awk: ccstscan.awk:9: END { for (i,j) in a awk: ccstscan.awk:9: ^ syntax error this is what is mentioned in GNU awk manual: To test whether a particular index sequence exists in a multidimensional array, use the same operator (in) that is used for single dimensional arrays. Write the whole sequence of indices in parentheses, separated by commas, as the left operand: (subscript1, subscript2, ...) in array i

gawk / awk: piping date to getline *sometimes* won't work

无人久伴 提交于 2019-11-30 20:22:44
I'm attempting to convert dates from one format to another: From e.g. "October 29, 2005" to 2005-10-29. I have a list of 625 dates. I use Awk. The conversion works -- most of the time. Hovewer, sometimes the conversion won't happen at all, and the variable supposed to hold the (converted) date remains undefined. This always happens with the exact same rows. Running `date' explicitly (from the Bash shell) on the dates of those weird rows works fine (the dates are properly converted). -- It's not the textual contents of those rows that matters. Why this behavior, and how can I fix my script? Her

Peek at next line, but don't consume it

丶灬走出姿态 提交于 2019-11-30 17:52:51
getline reads in the next line and increments the NR counter by 1. After using getline , awk resumes work with the next line. This is the desired behavior in most cases. In my special situation I need only to peek the next line and depending on its content I read the next line or I need to backtrack one line. How can I backtrack one line in awk ? I tried setting the NR counter manually to NR=NR-1 but this doesn't work. Or is there a method that only looks at the next line without changing NR ? I need a lookahead of one line. Simply saving the line in a variable and referring to it later does