A program has the output set to 2 decimal place floats, one on each line in the file. Depending on the execution, many files can be output, each with the filename cancer.ex#, where # is the number of times the program was executed from a script.
The professor has provided an awk script as the first step to generating a 95% confidence chart using gnuplot. I'd like to convert the output to to the format
conf $1 $2 $3 var#
where # is the number from cancer.ex#
I've developed the following:
#!/bin/bash
Files=Output/*
String
for f in $Files
do
String="conf "
cat $f | while read LINE
do
String="$LINE "
done
echo $String
done
I know a number of steps are missing, as I've just started putting this together. My problem is executing the concatenation part, as it simply doesn't work. There is no output, nada when executing the script above. However, if I change String="$LINE
to echo $LINE
, then I get all the output of the files put on the terminal.
Is there a workable appending function for variables inside a loop in bash?
Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.
String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other.
The cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix-like operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.
In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball".
#!/bin/bash
Files=( Output/* )
String
for f in "${Files[@]}"
do
String="conf "
while read LINE
do
String+="$LINE "
done < "$f"
echo $String
done
The subtle difference with < "$f"
instead of piping cat $f
is mainly, that the while loop would execute in a subshell due the pipe, and the variable in the for loop would not actually be updated because of the subshell.
Note also, how, at various points I made the filename handling more robust (accepting filenames with spaces, e.g.)
That all said, I suspect you might be done with simply
String="conf $(cat Output/*)"
#
String="$(for a in Output/*; do echo "conf $(cat "$a")"; done)"
Proof of concept with dummy data:
mkdir Dummy
for a in {a..f}; do for b in {1..3}; do echo $a $b; done > Dummy/$a; done
for a in Dummy/*; do echo "conf " $(cat $a); done
Output
conf a 1 a 2 a 3
conf b 1 b 2 b 3
conf c 1 c 2 c 3
conf d 1 d 2 d 3
conf e 1 e 2 e 3
conf f 1 f 2 f 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With