I have a csv file like, let’s say table.csv
foo;3
bar;1
and a script
#!/bin/bash
[..]
# increment ${searchterm} by one
eval [..]
# decrement ${searchterm} by one
and my problem is to replace the right line and value with the incremented integer. In addition: If the searchterm is not in the file, it should be assumed as 0.
The goal is to have a machine readable overview of the number of processes in different the eval-statements.
awk or sed might be solutions but I didn’t find a clue.
Edit: Hi, as @shellter’s answer already solved my problem I only try to get it more precise for documentation.
With the input “foo” I wanted to increment to
foo;4
bar;1
and after running through the eval-statement get back to the original values.
With the input of “demo” I want to get an output like
foo;3
bar;1
demo;1
and afterwards back to
foo;3
bar;1
demo;0
afterwords.
The idea behind is to have the whole script running in several instances at the same time and to be able to read the number of instances from the table.csv.
given your data, and your sparse description (it is always good to show the output you need/expect AND and any error messages you are getting), try this
I use
incr=3to show that this is a generic solution. If you need a+1increment, just change the argment toincr=1.Some awks want to see the command-line variables set as
-v target=foo -v incr=3.You can easily change this bash script to accept arguments for the target and incr, input file as $1 $2 $3.
Right now the output just appears on your screen. I’d recommend that you redirect output to a new file.
Note that FS is the awk special var that means FieldSeparator. OFS is OutputFieldSeparator, so it is possible to easily convert to true CSV in your case by changing OFS to OFS=","
Awk parses each line of input into fields; the whole line is refered to as $0, while each element as delimited by FS (in your case ‘;’), is assigned a sequential number moving from left to right, so the first field is called $1, 2nd is $2, etc.. Awk has many other internal variables including NF (Number_of_Fields), which is set for each line of data read. You can use NF by itself to validate you have the correct number of Fields in a line (for one example) or you can ‘dereference’ the value of NF with the ‘$’ char and have access to the last value on each line with ‘$NF’. For you example, we could rewrite $2 as $NF.
The default action for awk, given code in { .. } blocks, is to read each line of input, and then process it as dictated by the logic inside the block. In your case, we look at field one to see if it is the target string, and if it is, we increment the value there.
For all records, we print out the complete line, whether it has been incremented or not.
A BEGIN block of code is executed before any records from the input files are read, and is a way for variable initializations to happen (or in this case, you are overriding the default value of FS).
A END block is executed after all input files are read. As you need to print foo;0 if it is not there, we keep a flag to indicate whether your search target has been found or not. The last thing we do is check to see if target was found and if not, emit a line with ‘target OFS 0’ as text.