How to run all input test files from a Makefile? - bash

This may be a very noob question, but I have no idea of how to solve it. I have a C program and a folder called "input". Is it possible to write rules that allow me to type "make run" and then execute all test instances, writing the result into a single file?
I imagine that it's something like:
run: test1 test2 test3
test1:
./myproj < arq1.in > output.out
test2:
./myproj < arq2.in > output.out
...
But how do I achieve that everyone writes into the same file, ie, appending the results, instead of rewriting it? Is it possible?
Thanks in advance

First switch to ">>" to append to the output file:
run: test1 test2 test3
test1:
./myproj < arq1.in >> output.out
test2:
./myproj < arq2.in >> output.out
test3:
./myproj < arq3.in >> output.out
Then make those targets PHONY, because they are (i.e. they do not build files named "test1", "test2" and "test3"):
.PHONY: test1 test2 test3
Then switch to a static pattern rule:
run: test1 test2 test3
.PHONY: test1 test2 test3
test1 test2 test3: test%:
./myproj < arq$*.in >> output.out
Further refinements are possible, but that's enough for now.

Related

Can I have yml anchor in a different file

I need to have an "anchor" file and then several files that "inherit" from it. is it possible?
file1:
tests: &flow1
flow:
- simulaor
test:
- test1
- test2
- test2
file2:
run_tests:
<<: *flow1

How to concatenate variable in Makefile recipe

I get this Makefile:
LIST = foo bar
$(LIST):
echo $#
When I run make targets the outputs works as desired:
$ make foo
echo foo
foo
$ make bar
echo bar
bar
But if I concatenate a string another-, only another-foo works:
LIST = foo bar
another-$(LIST):
echo $#
$ make another-foo
echo another-foo
another-foo
$ make another-bar
make: *** No rule to make target 'another-bar'. Stop.
How can I concatenate a target to expand to all values in my variable?
The behavior you observe is standard: when writing
another-$(LIST)
make just replaces $(LIST) with its content, yielding another-foo bar.
That is, this has nothing to do with, e.g., a bash brace expression such as another-{foo,bar}.
Yet, you can achieve what you want by doing something like:
LIST = foo bar
$(addprefix another-,$(LIST)):
echo "$#"

Print the first N lines of a CSV where quoted fields can contain newlines

CSV file can have data with new line . it can be with any column. Also some line can have data without any new line so it should work in all case
Sample input
ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect
Thanks for your time!
With Joy.
Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect
Thanks for your time!
With Joy.
Test",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111113,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111114,TestUser,1234567891,test1,hello msg1,Address test2,City test2
I am using below command to read top 5 record of csv
awk -v RS='("[^"]*")?\r?\n' 'NF{ORS = gensub(/\r?\n(.)/, "\\\\n\\1", "g", RT); ++n; print} n==5{exit}' file.csv
Actual output:
ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect\nThanks for your time!\nWith Joy.\Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111113,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111114,TestUser,1234567891,test1,hello msg1,Address test2,City test2
Wanted output:
ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect\nThanks for your time!\nWith Joy.\Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2
With your shown samples only, could you please try following awk code. Written and tested with GNU awk. Make use of RS record separator and then substitute globally to nullify new lines in RT, then print the lines accordingly.
awk -v RS='"[^"]*"' '{gsub(/\n/,"\\n",RT);ORS=RT} 1' Input_file
To get first 10 records, try following:
awk -v RS='"[^"]*"' '{gsub(/\n/,"\\n",RT);ORS=RT} 1' Input_file | head -10
Warning: Self promotion ahead!
I wrote an awk-like utility called tawk that uses tcl as its scripting language, and has a mode that reads CSV data without having to hack around with regular expressions to handle records with embedded newlines and quotes (This feature was actually my major inspiration for it).
Using it:
$ tawk -csv 'line {$NR <= 5} { puts [regsub -all {\n+} $F(0) "\\n"]; if {$NR == 5} exit }' input.csv
ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2

Accessing Makefile variable in compile time shell

I have a makefile, 1.mk with following content:
#!make
aa := x
bb := y
cc := z
export
include abc.mk
all:
#echo $(chk1)
And another makefile, abc.mk with following content:
#!make
chk1 := $(shell set -o posix; set | awk -F "=" 'BEGIN{ORS=" "}1 $$1~/[a-zA-Z_][a-zA-Z0-9_]*/ {print $$1}')
chk2 := $(shell env &> pqr)
export
When I check, none has the makefile-variables:
> make all -f 1.mk | grep aa
> grep 'aa' pqr
Thus I can say, compile time shell does not have makefile variables. I want a way to access all makefile variables in compile-time shell.
Constraint: I do not know variable names beforehand to write code in following manner:
chk2 := $(shell export aa=$(aa); env &> pqr)
As far as I understand the way make exports make variables, it does so only in recipes, not in the shells invoked with the shell function. You can test this with, for instance:
$ cat Makefile
aa := x
export
chk := $(shell echo $$aa)
.PHONY: all
all:
#printf 'in shell function: aa = $(chk)\n'
#printf 'in recipe: aa = %s\n' "$$aa"
$ make
in shell function: aa =
in recipe: aa = x

Literal text not recognized in if statement

I'm trying to spit this text out but the view is not recognizing the text "Test1 Test2, test3 test 4":
<h3>#if(#user != null){#user.FullName Test1 Test2, Test3 Test4 #user.LastSignIn.}</h3>
You might want to surround it with <text></text> tags
<h3>#if(#user != null){#user.FullName <text>Test1 Test2, Test3 Test4 </text>
#user.LastSignIn.}</h3>

Resources