I'm creating a script that performs the following modifications:
DAY_OF_WEEK. In the file it is stated, 1,2,3, etc. I want to replace this number by the particular day. For example, 1 = Sunday, 2 = Monday, etc.
ORIGIN: In the file the ORIGIN it is written between "". I need to remove them. For example, "ORD" would be ORD.
I've done the following:
#!/bin/awk -f
BEGIN{FS = ","}
{
if ($2 == 1)sub($2,"Sunday"); print
if ($2 == 2)sub($2,"Monday"); print
}
END{}
However, my problem is that I don't know how to link many multiple conditions that affect a single print. In this sense, the output must be a new file containing the modifications carried out. Instead, what I'm getting is the file twice: 1st time with the replacement of 1 by Sunday and second one with the replacement of 2 by Monday.
For many of us, it's generally accepted that we have have a CSV file (like yours) that we convert the CSV files into an array of hashes.
Then, we do simple operations on the array when we want to manipulate or transform the data.
For example, in Ruby, we can use the builtin CSV parser:
Python and PHP also have similar "built-in" CSV parsers, which cover the original CSV file to an array which can easily be processed. For example, in Python we import the CSV libs, as follows:
import csv
As you might imagine, reading, parsing and writing CSV data is very common in 2021; having been around for a long time, and so most codes have basic tools and libs for this standard type of CSV processing.
Personally speaking, I use Ruby for this type of CSV task, and I use a Ruby gem called smarter_csv, but that's because I like processing CSV files with Ruby:
EDIT: OFS must be set because an assignment to a field causes a reformatting (re-split on OFS).
EDIT2: split() splits ob FS by default. I use a space, so FS should be set to comma after that!
Yes. While sub (r,s,t) operates on (and modifies) the target immediately, substr (s,i,n) reads the operand and supplies the result as its return value. So try
$5 = substr($5,2,3)
in your script.
Be aware that your statement
is not quite true. As sub - if t is missing - operates on $0, the entire record, in your second data line the "DAY_OF_MONTH" field will be substituted by "Monday", because it is the first matching field.
why all these nested/convoluted/hard-wiring just to translate "day of week (1..7)" to "locale's full weekday name (e.g., Sunday)"?
I thought @MadeInGermany in this post has made a very reasonable suggestion to use an array of "weekday names"....
@Panri93,
Any particular reason you cross-posted the same question on SE while you were suggested a very similar approach by @MadeInGermany in this thread here?
Please don't cross post the same questions on multiple forums while you were given a very reasonable suggestion here. This type of behavior is highly discouraged and frown upon.
Maybe my fault - I had a bug that now is corrected.
I let split() use FS and a string with space separators, so FS is to be set to comma after it.
The alternative would be to first set FS to comma, and then split a string with commas.