> I don't plan to add network functionality to this (even though you totally can), so no clone or push.
Git clone and push also work with local repositories, they don't require network functionality anyway.
My main complaint here is that there's not even a single mention of GNU AWK's networking features.
Actually no, I take that back. My main complaint is that GNU AWK has networking features.
I have the language for you: awk. Every variable that is not a parameter of a function is automatically global! Good luck with using i as the standard loop variable.
That's a full-blown extension. A much simpler thing for simple cases is to simply use FPAT variable (available in GAWK).
The man pages are just a reference. They are really good if you actually know the language. If you need to learn the language just read the actual manual for gawk: https://www.gnu.org/software/gawk/manual/gawk.html
The simplest way would be to use awk instead of grep:
$ awk ' /input_userauth_request:/ { print $9 }'
The first part of the awk argument looks through the file for the string "input_userauth_request:" while the second part prints the ninth word in any line which matches.
Naturally, this will break in weird and unpredictable ways if the string "input_userauth_request:" shows up in any other context, or if the date or source address ever has a different number of spaces in it. If you want something more robust you can do some more detailed pattern matching, but this will probably work almost all of the time.
The book with two lorises on the cover would be helpful, as would the short, to the point manual although there are plenty of other references available.
It seems that your comment contains 1 or more links that are hard to tap for mobile users. I will extend those so they're easier for our sausage fingers to click!
Here is link number 1 - Previous text "awk"
^Please ^PM ^/u/eganwall ^with ^issues ^or ^feedback! ^| ^Delete
> Edit: reading about awk, maybe it's a bad example because it turns out it's a whole programming language.
It still points the problem: the bundled documentation isn't friendly enough and you have to resort to something online. GNU themselves provide an ebook manual for awk.
Man pages are good and all if you need a reference. But if you want to learn something from 0, a book (physical or not) is a much better format.
Another good example would be groff
. At the end it points you to another lot of man pages so you can get more confused.
That's a dynamic regular expression, which invokes at least one more extra layer of conversion/interpretation than using an AWK regular expression literal. Specifically:
> When using the ‘~’ and ‘!~’ operators, be aware that there is a difference between a regexp constant enclosed in slashes and a string constant enclosed in double quotes. If you are going to use a string constant, you have to understand that the string is, in essence, scanned twice: the first time when awk reads your program, and the second time when it goes to match the string on the lefthand side of the operator with the pattern on the right. This is true of any string-valued expression (such as digits_regexp, shown in the previous example), not just string constants.
Very cool. Just to let you know, gawk, or GNU awk, has been around for a long time. Much different use case but be aware in case you’re worried about project name clashes.
GNU/awk has a FIELDWIDTHS option that lets you specify field splitting with an array of column numbers.
www.gnu.org/software/gawk/manual/gawk.html#Fixed-width-data
Edit: Usage Note: Field splitting takes place before your user code gets to see the line. So (like FS) setting FIELDWIDTHS in the line action is too late. It needs to be set in a BEGIN { .. } block.
The best thing is to memorize a few POSIX awk recipes which are good in specific situations, and not try to push the language to its limits. There are dark corners of the specification which can really mess with your head when you fall into them.
Another thing to realize is that awk is highly optimized for certain kinds of work. Anything which involves reading newline delimited records split into whitespace separated fields is a good candidate.
Hmm, that's the warning I got when I used '\s*=\s*'
, so I changed it to '\\s*=\\s*'
and it worked for me. Seems like it's the opposite for you. You could maybe use '[ \t]*=[ \t]*'
instead.
But yeah, awk
is quite powerful. It's a great tool to have in your bag when you're messing with text files. I mainly learned from these:
The official description of FPAT, from the GAWK manual, is at https://www.gnu.org/software/gawk/manual/html_node/Splitting-By-Content.html.
gawk -vRS='>Cluster' -F '\n' 'NF == 3 { printf "Cluster %s", $0 }'
Plain old awk (FreeBSD) doesn't want to play like that?!
Ohh... https://www.gnu.org/software/gawk/manual/html_node/gawk-split-records.html
From the GNU awk manual:
> There is an important difference between the two cases of ‘FS = " "’ (a single space) and ‘FS = "[ \t\n]+"’ (a regular expression matching one or more spaces, TABs, or newlines). For both values of FS, fields are separated by runs (multiple adjacent occurrences) of spaces, TABs, and/or newlines. However, when the value of FS is " ", awk first strips leading and trailing whitespace from the record and then decides where the fields are.
So all the other variants you are using, simply wont strip leading and trailing spaces.
Is this just a general question and 'cat -n' just an example or are you really trying to extract line numbers from the cat output?
yup, https://www.gnu.org/software/gawk/manual/gawk.html is a complete reference and top notch learning material
I've added reference to relevant sections throughout the chapter
there are other good learning resources as well like grymoire which I've collated in further-reading section
In general (across languages), adding .5 to a float and casting it to int is a fairly widespread method of "rounding as is taught in schools" (to steal a phrase from AppleScript). The x86 processor will use ADD, then FIST or FISTP. So, it's a very quick low-level way of doing it. So, yes it looks terrible in BASH, but it is generally the best performance-wise.
Yes, it adds .5 to the value, then truncates all the values after the decimals. so 1799.49 + .5 = 1799.99 and is truncated to 1799, while 1799.5 + .5 = 1800.0 and is truncated to 1800.
That said, awk 'BEGIN { printf ("%.0f\n", 1799.6);}'
seems to work for me, with the caveat I've added the .6 there. If it's not working, it probably means you need to specify a ROUNDING MODE, but the default ROUNDING MODE (N - to nearest, ties to EVEN) should work properly here.
That is a way to win. And awk is fun. It's really good at what it does, parsing and modifying text, and has so much utility that any time invested in it is not only fruitful but probably bountiful. There is a vast army of people who are barely able to use awk at all yet they do use it to good effect. I'm quite fond of awk.
Awk - A Tutorial and Introduction - by Bruce Barnett
I found that useful. I'm one of those people who cobbled together cheap awk one-liners for decades. If it took more than that, I went to Perl, which is a lovely language. But eventually, I got ensnared by awk, which is both radically utilitarian and simultaneously quite commodious if you ever do really get into it. And it's got that old school cool vibe going for it, which is really the only vibe I have any hope of pulling off.
Gawk: Effective AWK Programming
That's the GNU AWK manual, which is definitely not GNU's worst manual. It is a useful reference and is complete. It's a good place to lookup string functions, which are useful for manhandling your text.
The awk man page is handy as well.
​
awk '{ sub(/\s{34}/, " "); print; }' \
< wash1889_final_parts_0.txt \
> shortened_indents.txt
That will reign in those extreme section indents without abolishing them entirely.
awk '{ sub(/\s{34}/, " "); print; }' \
< wash1889_final_parts_0.txt \
> chomped_indents.txt
That would abolish them entirely.
if you just need to call external commands, you can use the system() function, as in: system("mv \"name1\" \"name2\"")
if you need to process (on your awk program) the output from a external program called by you, you can use getline() from a pipe, but I don't think your example needs the extra layer of complexity.
Just use Python's CSV library or any other language with proper CSV support. Awk doesn't count, by the way! (See the note near the bottom of this page.) CSV is a hell format with many competing "standards" so there's no guarantee you can properly parse your data, but even if you're super sure you can, there's a much bigger issue here: Bash can only do integer math. A single decimal point will break your script and you'll have to do it in something else anyway. Just use a tool that's built for this kind of task and you'll save yourself a lot of pain.
Numbers don't "turn into" scientific numbers. That is an artefact of the printf default format when you display them. If you allow them more precision, you will get more digits.
Awk stores all numbers as IEEE 64-bit (double) precision. Typically, the default is to show you no more than six digits after the decimal point.
GNU/awk does have an inbuilt high-precision library (it is a compile-time option, so check your gawk has it). Take a look at:
https://www.gnu.org/software/gawk/manual/gawk.html#MPFR-features
Your comparisons probably are being treated as strings for comparison purposes. Variables do not have a type assigned until they get a specific assignment.
For the full story, see this (and cry):
https://www.gnu.org/software/gawk/manual/gawk.html#Variable-Typing
The standard trick is to force a result to become a specific type by combining with a variable of that type.
(0 + myValue) #.. converts to numeric as much of myValue as it can.
("" myValue) #.. appends the string value of myValue to the null string.
myValue += 0; #.. forces myValue to be treated as numeric in future.
$ echo 6.34e+03 | awk '{ print $0; print 0 + $0; }' 6.34e+03 6340
Awk array indexes are always strings.
If you use an integer index, it will be converted as if with a printf "%d" format.
If your index is a real (float-type) number, it will be converted to a string using the format in CONVFMT (defaulted to "%.6g"), which creates a value with at most six significant digits.
Check out www.gnu.org/software/gawk/manual/gawk.html#Strings-And-Numbers
If you want to access arrays as ordered by a numeric index, every index needs to be the same numeric format. You can change CONVFMT, but it is a global variable.
I prefer to explicitly sprintf my indexes with my own format. Typically, I will scale the value and use decimal with leading zeroes.: 34 becomes 00340000, 3.14 becomes 00031400, depending on the data range. Negative indexes are a special case too.
I'm rusty with awk, but I think you can replace your |grep|grep|cut|cut|sed
with | awk '$0 !~ /Monitor/ && $0 ~ /Built-in/ { print gensub("source-([0-9]+),", "\\1", 1, $3) }'
Another way to do it: | grep -v Monitor | grep -oP 'source-\d+' | cut -d- -f2
. Grep's -v
outputs lines that don't match, -o
only prints out the part that matched, and you probably can figure out the rest.
Some notes about the awk "solution"... $0
contains the whole line, $3
is the 3rd column of the line ("source-44,"), ~
is a regex match, and !~
translates to "doesn't match the regex". Also, you can find the docs for the gensub string function here.
I'm going to bed now, so if you have questions, I can answer it in 8 or 9 hours.
Another good resource for learning AWK is The GNU Awk User Guide. Not only does it cover the AWK language in detail, it also includes a section on "Problem Solving with awk
" with example code for several common tasks.
For a Ryzen CPU.
Finding hwmon (because ../hwmon(n) can change after a upgrad/update)
#$ find /sys/devices -type f -name "temp?_label" -exec grep -H . "{}" ";" 2>/dev/null /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp4_label:AUXTIN1 /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp8_label:PCH_CHIP_CPU_MAX_TEMP /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp1_label:SYSTIN /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp5_label:AUXTIN2 /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp9_label:PCH_CHIP_TEMP /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp2_label:CPUTIN /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp6_label:AUXTIN3 /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp3_label:AUXTIN0 /sys/devices/platform/nct6775.2592/hwmon/hwmon2/temp7_label:SMBUSMASTER 0 /sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon1/temp1_label:Tctl /sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon1/temp2_label:Tdie /sys/devices/pci0000:00/0000:00:01.1/0000:01:00.0/hwmon/hwmon0/temp1_label:Composite /sys/devices/pci0000:00/0000:00:01.1/0000:01:00.0/hwmon/hwmon0/temp2_label:Sensor 1 /sys/devices/pci0000:00/0000:00:01.1/0000:01:00.0/hwmon/hwmon0/temp3_label:Sensor 2
${color1}Tdie:{alignr}${color green}${hwmon 1 temp 2}°C ${color1}Tdie:{alignr}${color green}${execi 2 sensors -f| awk ' /^Tdie/ { printf "%3d°F" ,$2 ;exit}'}
https://www.gnu.org/software/gawk/manual/html_node/Format-Modifiers.html
>That's not an operator but part of compilation.
True. AWK also doesn't treat it as an operator.
>There is only one string operation: concatenation. It does not have a specific operator to represent it. Instead, concatenation is performed by writing expressions next to one another, with no operator.
https://www.gnu.org/software/gawk/manual/html_node/Concatenation.html
​
>You can't concatenate two string variables.
It depends on what you mean by "string variables". If you have two variables of type std::string you can concatenate them using the + operator.
GNU awk versions fwiw:
gawk 'BEGIN { PROCINFO["sorted_in"] = "@val_num_asc" } { for (n = 1; n <= NF; n++) grades[n] = $n for (g in grades) printf "%d ", g print "" delete grades }' grades.txt
or
gawk '{ for (n = 1; n <= NF; n++) grades[n] = $n + 0 asort(grades) for (n = 1; n <= NF; n++) printf "%d ", grades[n] print "" delete grades }' grades.txt
References: https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html and https://www.gnu.org/software/gawk/manual/html_node/Array-Sorting-Functions.html
I wouldn't recommend most of the tutorials or courses available on web, whether free or not, they're mostly shit, scratch the surface of the surface, and over-simplify things so that brain-dead readers can understand.
I'd recommend that you first read the 'Effective awk programming 2015 ' book and then when you have time, give the standard gnu awk manual at https://www.gnu.org/software/gawk/manual/gawk.html a go.
Can't figure out how to directly link to the section, but search for "system(command)" on this page
Let's you do stuff like "if this AND this, create this file here, with this info, then send an email"
The beauty of Linux and Unix tools is just this: they are small, and from a surface (relatively) easy. Regex is the same: small and (relatively) easy. You can learn basics pretty fast and be able to do something useful with them.
But if you start to think about it, it surely isn't "just" regex, but actually a really nifty little language with lots of quircks and features. You can work with regex (and sed and whatever) ten or twenty years and still find new things. Awk on the other hand, is a language of its own. Definitely not just regex.
Btw, did you know GNU awk has also TCP/IP support? https://www.gnu.org/software/gawk/manual/gawk.html#TCP_002fIP-Networking
Perhaps we're both right :-)
At the end of the day, nothing is "posix" per-se (because posix is a standard, not a 'thing'), but some things are posix compatible with flags, and some things are posix compatible by default (e.g. sh/dash).
And sometimes, like gAWK, the posix compatible with flags is annoying, but other times, posix is too restrictive so the flags are nice.
But, if you want to blow a gasket, awk is covered by posix, but check out all the various implementations: https://www.gnu.org/software/gawk/manual/html_node/Other-Versions.html
I mean, if you're going to link to a source about lets say awk, why not link to the official GNU docs instead of some videos that aren't even free?
I agree with OP that it is in poor taste.
I was about to suggest my one-liners book (https://github.com/learnbyexample/learn_gnuawk) - while it covers from basic to some intermediate/advanced examples, it may not fit.
https://www.gnu.org/software/gawk/manual/gawk.html is a book/manual combo which covers every aspect of awk
It would help if you give some example use case or describe some portion of your problem statement. Python in general is slow compared to awk but if you are processing specialized structure like csv, then you are better off with tools like https://github.com/BurntSushi/xsv or even the pandas library with Python instead of awk.
If your input is ASCII, then LC_ALL=C
will give you significant speed boost.
you can use logical operators to combine multiple conditions
awk -F, '/bar/ && length($2) == 6{print $2}'
See https://www.gnu.org/software/gawk/manual/gawk.html#Boolean-Ops and https://www.gnu.org/software/gawk/manual/gawk.html#Bitwise-Functions
What have you tried? Have you looked at the documentation for the for statement? Have you tried writing a program that just prints the numbers 1 to 100?
You can do it within awk
itself, provided you understand all the caveats of using getline
(see https://www.gnu.org/software/gawk/manual/gawk.html#Getline and https://awk.freeshell.org/AllAboutGetline)
$ cat f1.csv a,b,c d,e,f g,h,i $ cat f2.csv 1,2,3 4,5,6 $ awk -F, '{ printf "%s,%s", $3, $1; if((getline < "f2.csv")==1){ printf ",%s,%s", $2, $1 } print "" }' f1.csv c,a,2,1 f,d,5,4 i,g
The logic itself is wrong. If it is #, then it isn't y. If it is y, it isn't #. At least two of the tests will be false.
Also, if the actions for the tests are different, you have to separate the tests. All this could do is test whether any of the conditions is true: you still need to retest to find what to actually do.
You should quote $3 as "$3" every time, in case it includes spaces.
As Osrs says, the right construct for this is a case.
expr is way out of date. Bash does substr internally. There are about 40 smart things it will do to arguments. Open this https://www.gnu.org/software/bash/manual/bash.html and do a browser search for: parameter:offset. Scroll up and down some for other options.
Then leave the tab open, because it is the best manual I know (with the possible exception of www.gnu.org/software/gawk/manual/gawk.html ).
Looks like your formatting got a bit mangled, but sounds like sed will be perfect for you.
Presuming that the start of each line is a first name, you could filter for lines with the pattern ^June
, which would only print lines where the first name is June.
Not quite sure what you're aiming for with the are code one, but potentially something to consider is awk, which will, by default, split each line according to spaces, but you can specify your own field separator.
For example, feeding your file into awk -F, '{print $2}'
would print the second field, your zip code. You can also use regex in awk to test whether a field matches a pattern, like so:
awk -F, '$2 ~ /123-/ {print $2}'
This line would print only those lines where field 2 (the zip code) matches the pattern 123-
. You could add a ^
to the start of this pattern to only look for zip codes starting with that pattern.
This should be a good start for you!
As far as I know, there's no such thing. Awk is slower (not slow) because it's much more powerful than sed/grep. So when he says avoid awk when you can, it's the same as avoid using a nuke to kill the spider in your bedroom. And finally, for text processing, you're IO-limited like 90% of the time so don't sweat the small stuff.
The gawk
reference has a whole page about this specific problem. Is your data actually double-soft-quoted and wrapped in soft quotes, or is this just how you have presented it?
readfile() as a function that can work in any awk version, the gawk specific feature is that it's included by default with the language, and can imported with -i
https://www.gnu.org/software/gawk/manual/html_node/Readfile-Function.html
awk
is an entire DSL (domain-specific language), so it's hard to sum it up. The GNU AWK User's Guide is a great resource, but very long. https://www.linuxcommands.site/linux-commands/awk-syntax/ seems to be a decent tutorial, although you'd probably need programming experience to understand it.
By the way, you can replace both grep
s by expanding the awk
call; let me know if you want to try it as an exercise or if you just want the solution.
I think you want to use a single ">" for the redirection in awk. i.e.
print $1 > "/etc/pihole/gravity.resolved"
A single ">"
in awk creates/overwrites the file on the first call, and then appends new lines on subsequent runs of the loop (unlike bash, where single redirection would overwrite the file each time). Double redirection ">>" will append lines even on the first call, so if you run your script multiple times, you will get duplicate records and the file will grow and grow each time.
See the gawk manual and the mawk manual (mawk is the default awk in debian/raspbian)
Additionally, it is good practice to close the file descriptor in the end, so you may want to append this line at the end of the script:
END { close("/etc/pihole/gravity.resolved") }
(although, gawk may close the file descriptors on its own, I don't know about mawk)
Another, simpler approach is to leave the awk code as it is (i.e. just print to the standard output), and redirect the output when calling the script from the shell:
cat hosts.list | ./filter_dead_domains.sh > "/etc/pihole/gravity.resolved"
>The expression: $3 ~ /'$*'/ searches field three for the pattern passed from the command line.
This is a bug and potential code injection. One way to pass data to awk is via its -v option:
awk -v "pattern=$*" '$0 ~ pattern { ... }'
Although it seems better to use a particular parameter like $1 instead of $*.
How would you find out about a film, a restaurant, the weather? Heard of Google yet??
Google search "Posix standard". It will tell you that Unix/Linux has a standarised set of functions (lots of systems have awk extensions, but the Posix standard defines a subset that should be portable anywhere).
Now google for posix awk, and go for the Open Group Library document. Shouldn't be hard -- it came up as all my top 3 searches.
Search for "examples". My Opera browser uses a keyboard short-cut Ctrl-F to open a search box (so does FireFox), and says there are 20 matches. Click on the > until you reach the EXAMPLES header. Then scroll (PageDown key) until you read example 13.
Actually, there is a bug in that example. You will get bonus points if you fix it. The bug is that if the input file is empty, it will crash out on a divide-by-zero.
You are not going to learn anything by bootlegging answers from the web, though. You need to grok it. (Another google there, then.) I can explain it for you, but I can't understand it for you.
I would suggest you open this magnificent document in your browser:
https://www.gnu.org/software/gawk/manual/gawk.html
and then work your way through all 19 Posix examples, looking up everything you don't understand.
It is worth it. Awk is one of the best, fastest, and most powerful Unix commands, and a very good first language to learn.
Maybe a glance at String Manipulation in the manual is the way to go instead of trying to shoehorn something you do know into something you don't....
Check out
substr(string, start [, length ])
For Awk you should start by reading the GNU Awk User's Guide. After this you should be pretty proficient in the language. At the end there are some example scripts. Awk kind of leads you to certain kinds of solutions for problems so whenever you need to write a script for something you could consider writing it in Awk and get the practice that way.
> Can <blah> be a regular expression?
Yes, and in GNU awk it will match the exact string <blah>
since the angle-brackets aren't special in the GNU awk regex dialect. More information.
Well it depends what you mean by universal. For Linux flavors generally gawk is available. You can also rely on most GNU core utils to be installed. I recommend you reading up and learning gawk since that has the most features. The other awk versions have similar or fewer features.
I recommend reading at least Part I section of the gawk manual. It will help you along drastically with writing your first script and understanding what the script is doing. Awk is one of the simplest languages I know and definitely one of my favorites, though, very unlike any other language.
https://www.gnu.org/software/gawk/manual/html_node/Getting-Started.html#Part-I_003a-The-awk-Language
You encounter BSD awk on BSD Unix and other operating systems such as Mac OS X. If you want your awk scripts to be portable, then learning what BSD awk and GNU awk have in common and only using those features will keep your script portable.
For other standard utilities on Linux there’s GNU core utils which has its own manual you can read.
https://www.gnu.org/software/coreutils/manual/
Side note: (not necessarily awk)
I plan to write a blog post on simple rules for writing POSIX compliant scripts.
When writing portable scripts you have to create a lot of logic to determine what utilities are available and using minimal utilities and options to keep it widely portable across Unix-like and Linux-flavor platforms. Since that’s off topic of you learning awk I’m not going to go too much deeper than that for cross platform scripting.
There is XOR in awk, but I do not know how robust implementation can be with binary data etc. If you need it simple and robust go with your implementation. Edit: this link
yup, plenty of other good resources to learn from, especially when it comes to non-GNU version
and nothing beats https://www.gnu.org/software/gawk/manual/gawk.html as a complete reference
I've mentioned them in further reading section :)
awk is actually a full blown (shitty) programming language that was designed for text processing/data reporting. You can do most anything in awk that you could do in any mainstream programming language.
For an example of why I consider it shitty: https://www.gnu.org/software/gawk/manual/html_node/Variable-Scope.html
But really, if what you need to do with awk fits within a screen or two, or you routinely deal with log files that have to be cleaned up, awk is a pretty decent tool to know.
AWK can do floating point arithmetic to include complex math.
https://www.gnu.org/software/gawk/manual/html_node/Function-Calls.html
You can just use AWK in your BASH script but BASH does not have Internal support for this.
That's said, BASH is a system shell language not a general purpose one. I would recommend learning Python or Ruby for this task.
You will likely find it easier, in the long run, to use either awk
(www.gnu.org/software/gawk/manual) or sed
(www.gnu.org/software/sed/manual). Like /u/henry_kr, I recommend trying awk
.
> perl is also a program
But it is.
> Please link to anyone saying that invoking awk = a program
https://www.gnu.org/software/gawk/manual/gawk.html "This file documents awk, a program that you can use to select particular records in a file and perform operations upon them"
A quote to that effect was also provided in this comment and the link is in its parent.
> and it isn't a language
Nobody said it isn't a language, that's a straw man.
As they still sometimes say, RTFM:
> Normally, when using FS
, gawk
defines the fields as the parts of the record that occur in between each field separator. In other words, FS
defines what a field is not, instead of what a field is. However, there are times when you really want to define the fields by what they are, and not by what they are not.
>The most notorious such case is so-called comma-separated values (CSV) data. Many spreadsheet programs, for example, can export their data into text files, where each record is terminated with a newline, and fields are separated by commas. If commas only separated the data, there wouldn’t be an issue. The problem comes when one of the fields contains an embedded comma. In such cases, most programs embed the field in double quotes.
www.gnu.org/software/gawk/manual/html_node/Splitting-By-Content
Cheers!
Maybe use awk
? Apparently you can set the variable FIELDWIDTHS
to have awk
parse a fixed-width output, then use ye olde printf
to format things. (But beware the subtle yet infuriating syntactical differences between BASH's printf
and awk
's printf
.)
See:
Hmm, doesn't look like that's actually correct.
I think you're actually confused about your testing output here. Every single test output should end with true. The last three actually check that the method rejects strings that don't fit the pattern. Your output should really look like this:
nearby_az("baz") == true: true nearby_az("abz") == true: true nearby_az("abcz") == true: true nearby_az("a") == false: true nearby_az("z") == false: true nearby_az("za") == false: true
If the last three say "false: false", your method is approving strings that don't actually match.
Anyway, with regards to the regexp, try using the {} expression.
Here's a quick rundown on various regexp operators, including {}: https://www.gnu.org/software/gawk/manual/html_node/Regexp-Operators.html
You might-could just drop the anchor, i.e., grep 'EXTINF:'
. Unless you expect the string EXTINF
to show up elsewhere in each record.
BTW, the whole process would probably be a hell of a lot easier to do with <code>awk</code>, instead of all the futzing around with sed
and grep
.
I was a full-time student, doing some casual work at the uni. My supervisor/boss wanted a list of word pairs with very specific characteristics, but she couldn't figure out how to begin to compile such a list. But she was sure it should be possible using publicly available databases in combination with a programming language called AWK. So she says "Just learn AWK, and use that to do it". Now, if I was a computer science student who was handy with bash that would have been perfectly reasonable. However, I 1. was a psychology student who had never written a line of code in my life, and 2. was a Windows user who had never even seen a bash shell. So I get home, Google "AWK", download gawk for Windows, and start plugging away. I gave up around the time that I realised that DOS and Unix use different quoting rules. A couple of days later: "Have you got that word list yet?". I couldn't bring myself to bill her for the hours I spent trying to figure out how this whole computer programming thing worked.