Reputation: 111
I'm having a problem with reading multiple files and gathering info out of those files. Those files have an input like:
File1:
number1
text
number2
text
number3
text
File2:
number4
text
number5
...
I only want to get the numbers out of every file, and those numers need to be placed under each other, but only 1 time.
Like this:
number1
number2
number3
number4
number5
What I have:
var0=$(echo "test")
var2=$(echo "test2")
for file in*.txt
do
var1=$(awk -F .. do something "$file")
done
echo "$var0" "$var1" "$var2" >> myfile
So what i want it to do:
test1
here print var1 (so the numbers)
test2
The input files have something that i want to get out with the awk command, this works perfectly, when i echo the var i get number1,number2,number3,number4,number5,number1,... so it keeps repeating the command.
When i print all the var's he only gives the value of the last file in var1:
test
number4
number5
test2
And I want everything, so also number1, number2,...
Ty in advance!
Upvotes: 0
Views: 33
Reputation: 74655
You're overwriting your variable $var1
inside the loop, so it's no surprise that you only get the contents of the last file.
If I understand you correctly, you want something like this:
awk 'BEGIN{print "test"}/^[0-9]+$/;END{print "test2"}' *.txt
This reads all of the .txt
files in one awk command and prints any lines that only contain numbers (I'm assuming that's what you mean when you write number1, number2 etc.)
However if you would like to continue using your approach, I guess the only thing you need to do is concatenate the output of each awk, rather than overwrite $var1
:
var1+=$(awk -F .. do something "$file")
----^
If you don't actually care about the variable $var1
there's no need to use concatenation, as shown by tripleee in the comments:
{ echo "test"; for f in *.txt; do awk ... "$f"; done; echo "test2"; } >myfile
You can probably skip the loop too (which will be shorter and quicker):
{ echo "test"; awk ... "$f" *.txt; echo "test2"; } >myfile
...and if you can do that, we've arrived pretty much back at my first suggestion, which is to do the whole thing in one go using awk :)
Upvotes: 1