I want to check that I have not missed any entries, so I want to count the number of entries ( called fields) in a line, and check that it equals the number of days in a month.
Something like this
Have you looked at the “column” command… there’s also “cut” (I’m sure you know about “cut”)
I can remember using “cut” about 25 years ago… But when it’s simple columns of data these days, I always fall back to awk… Haven’t used “cut” since forever…
Here’s a quick and dirty script I wrote that used column to tabulate an even column output from a CSV file - I call the script “cli-excel” - maybe I should retitle it “cli-visicalc” or “cli-123” (I reckon I might!)…
#!/usr/bin/env bash
# read a csv in the terminal
# column -s, -t < eni-linux-list.csv | less -#2 -N -S
# expect an input file name
PROG=$(basename $0)
CSVFILE=$1
if [ "$#" -lt 1 ] ; then
echo "need argument - expecting a CSV file..."
echo "e.g. : $PROG filename.csv"
exit 1
fi
column -s, -t < $CSVFILE | less -#2 -N -S
Hmmm - but that doesn’t do anything like what you want… So you maybe count the number of spaces between each value - on each line +1
i.e. number of entries should be number-of-spaces-+1… Maybe? Then some kinda algorithm for getting the number of days in each month? Can you do that with the “cal” command (which I’ve already ranted about - how debian and ubuntu (and derivatives) no longer install cal/ncal by default - because IT’S SO HUGE they want save space (sarcasm)…
There’s a bunch of examples here (yes most seem to mention awk - but there are others) :
So the big trick, is to generate a multiline script which operates on one line of the file at a time. So in a backhanded way you used sed to do the looping.
I must admit I have never used sed to generate a script like that.
Looks like I need to buy your book.
Thanks, I am sure several people will be interested in how that works
Regards
Neville
The general technique here is turning data into commands, and piping the commands to bash. This technique really transformed the way I use Linux at the command line.
The trick is that bash is itself a command that reads from standard input, so you can send it strings to execute.
In my book Efficient Linux at the Command Line, I go into detail about this technique in chapter 7, which is titled “11 More Ways to Run a Command.”
You sold me, I ordered the book.
I’m a bit of a book fan. There was a time when being a Unix user meant having a shelf full of O’Reilly books. Things have changed with internet access, but there is still a place for well written learning books.
Regards
Neville