Hello all,
I have a question regarding a bash script:
how can I read a file and put in a array all the lines that are after each blank lines.
ex:
aaa
111
bbb
222
333
ccc
555
put in the array only aaa bbb ccc
Hello all,
I have a question regarding a bash script:
how can I read a file and put in a array all the lines that are after each blank lines.
ex:
aaa
111
bbb
222
333
ccc
555
put in the array only aaa bbb ccc
Use perl instead.
if it’s possible to call perl from my script it’s ok. but how I’m gonna make this task in perl. I don’t know perl.
is this a homework question?
–
brassy
No. I had to make a script to automate some stuff send to printer and I have to solve this task.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Here ya go:
<quote language=“perl”>
#!/usr/bin/perl -w
#Author: ab@novell.com
#Version: 0.1.20090721142000Z
#Description: Script hacked together to read every line that appears AFTER
a blank line.
#Note that lines with even a simple space on them will be treated as
non-blank.
#Pass in the file as the only argument.
if(scalar(@ARGV) == 0) {die("
" . ‘Specify the file to be read as a
parameter.’ . "
");}
my($readFile) = shift(@ARGV);
my($readNext) = 1;
my($fileLine) = ‘’;
my(@desiredArray) = ();
open(INFILE, “<$readFile”) || die('Unable to open file: ’ . $readFile . ':
’ . $!);
while(<INFILE>) {
$fileLine = $_;
chomp($fileLine);
if($readNext) {
if($fileLine =~ /^$/) {
$readNext = 1;
}#End if
else {
push(@desiredArray, $fileLine);
$readNext = 0;
}#End else
}#End if
if($fileLine =~ /^$/) {
$readNext = 1;
}#End if
}#End while
close(INFILE) || die('Unable to close file: ’ . $readFile);
print("
" . join("
", @desiredArray) . "
");
exit;
</quote>
Paste the contents (minus <quote></quote> tags) into a file and make it
executable. Feed it your text file as input. It prints output for you at
the end though that is optional and the output format could easily be changed.
Good luck.
ionpetrache wrote:
> if it’s possible to call perl from my script it’s ok. but how I’m gonna
> make this task in perl. I don’t know perl.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJKZc88AAoJEF+XTK08PnB53IEQAI4hDL5Esak/9opkDjK3OH4Y
4E1l7Q8BcAb6q0tmq5Gr0AF2kRb5ZTKN+F3mcNV60ag3qTPoEH+p+35CXNa3Fwx2
G/onBTviT3cJwQimmLmW8xKY7XXF84QXNOIzWW0trFHPGyfkRs/JDeScU+CvbfTE
m56g9DGH69zTIlF9V9xZmm0Eljn928RZsRjjik+/dTlpeGgEGE8xc9WHrpBJoW1e
nsaj9YBYoA8JO+lBeVh3SGk2uXBHpyV4GpelFt+/XKfJy14Pzbyy1trBXsRxrJwt
SfOh/izvyW45tHVohx107KkCPURvUPiRlNdJrwRQmjG4bK+wBHppNnuA1o1ukMdo
E2rNcqbhAt6Rzp1lvvB/0VPAzbpx3BxWF+2lVZQvQgN4NIJa+4AOLCyJkkQUqXsW
ADFIGwc+Bogg+xrpGWNDbnsJ2fNK42VkPDnaSQpS8Jd8mZzujKvq2O0xZl3iI7/e
6pCb0ofRKX6Tz2IMlO4/dfjvk82WfRbUQ+yLwsgVrhqvV0xEKOqy/ujZJ0TvLC2G
YP4mbvBO/SiOtejy/XfFMJi/5v+0ZF93KJgnBfIfu7reUgJuTGnXOXvMyh0DfDt7
IW8t3EEYJAbXpm233PbgRm8q/YwZpzG2sJSSumdd7/A+8YU9PKHa1oRONQgDpTBe
vL7MUwLeTn4SN3AQIMg5
=4/7+
-----END PGP SIGNATURE-----
Thanks a lot!
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Don’t just use it… learn from it. Perl is a great language to know
(cross-platform, very powerful for text hacking, free, open source) and
some of the basic concepts that it excels at (regexes, for example) apply
to many other languages.
Good luck.
ionpetrache wrote:
> Thanks a lot!
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJKZdjaAAoJEF+XTK08PnB5P3AP/0xc6iubIaO02qf9L8vjZbSS
7Bi1yMUMNzU+yPWgU1D/NmLotXyffiV01rD+xjpW4sBz5m4TKJ+NlqI49dLUEPk+
k2bcZDu6GGSPIAHTOjsGVCCf9LIXRGAbJV3ivJW5u7BkCKCRjTy9LzJlto1Tx/ph
U0Re8FLHrS73420KrbknA2A3vzLx/ZoEh5iHtdQr/SSanq6OmIfcnK/tU04uyojI
tp94j0YHnM4QQQhZEWS2BdEqB/fBc78OocxImj7a0xgA9lYRaFzHUvv8sBBLH7vc
uiPoUXslrZwkWFsOomLltW8c+ZOLL3dxLXNuuGDrVNBJ9w9JjF11rCCzg9Trl9Fg
352cthVuJanS1yBOOZAakIeJlL5Nc48KJ8Pn2w0DghjoSMjULRgn9u7/d3tZuABd
wSBL+CdB6EPUqQ0w6B2J+64L3X/79KBKJZFEBuY/wCl7xwAKpJJeiUrAPSmfm3qU
/ude6uurU5Vn/8unSxBADpXdvqHyJvGaQVCqui9Go1593QWnP3AHIBC0zw2GQud3
d3ZfmQupJhke9YpokvCAee+fCulAl39jph7Hxqz6dr8RqQuIbJFcGYO6Jr54ssoT
92cXkfdsk1NmdvBKIYlv6q0BLOM74X/LahzX5W3ATxIDD9pWM+01YmnhItXKordk
Lf6Xh6kGY3WoZxcvU7k6
=xxov
-----END PGP SIGNATURE-----
no need to. can be done with shell+tools.
fyi, please use code tags for posting code.
second, no need to overrate Perl.
use awk
awk 'BEGIN{ RS=""}{ print $1}' file
the above just print out what you want. Depending on what you want to do with them, there is sometimes no need to store them to arrays.
Sure, but why be a masochist. Besides when you mention tools, you are thinking of invoking external processes with attendant inefficiency. It’s far better to do it using bash built-ins. (Yes, I know how.)
You’re using awk, which is another programming language. The shell is there only to invoke awk. How is this different in principle from invoking perl? As opposed to doing it all in bash, using read and tests.
using bash builtin for parsing files is less efficient than using awk.
Yes, but you are simply proposing a different programming language, awk, instead of perl. Nothing wrong with that, but equally different from what the OP asked for, to be done in bash. Which was too restrictive a stipulation anyway.
And why would bash built-ins be in principle less efficient than awk? No external processes are involved.
first, “Use Perl instead” is considered “not a helping solution”. Anyone can post “Use Python instead”, or “Use Ruby instead”. but then it doesn’t really help OP with his current situation , ie bash. At the very least, if the poster wants to suggest “Use XXX language instead” , he/she should show examples of how it might help OP accomplish his task.
second, awk has been associated with the shell since prehistoric times, way before Perl. In this context as well as OP’s shell situation, what i say about “no need to use Perl” is not wrong.
third,
remember OP needs to send stuff to printer? most probably he might have to use things like lpr or some other command line tools for his particular printer. since that’s possible, using bash+tools might be more “the way to go”. (of course, i am not saying you can’t use Perl, but then you still need to make system calls to printer commands, or use some Printer module from CPAN or from Printer manufacturer etc).
the “main way” of parsing files, whether big or small, require the use of while read loop. (if there are any other built in ways, pls let me know, because AFAIK, this is the most common way). In bash, parsing files with while read loop is slower because this “loop” is not “precompiled” (if that’s the right word) into the bash executable itself. (ie you/me have to specifically write the loop). whereas with awk, its “loop” is already compiled into awk. You are a veteran, so i think you know what i meant.
Are you sure that it will make a significant difference in execution time, having to parse the while loop? Are you sure that bash doesn’t compile to an intermediate form before execution? Bear in mind there are also counteracting factors in using awk or perl. You have to load a program which is probably not in memory (at least the first time it is used), whereas the blocks for the bash interpreter are already in memory, thanks to Linux’s caching, so it can just share the executable code of an existing process.
To be honest I would not worry about these things and just write it in the language that is most convenient to code in (including awk if I feel like it) or most clear. If it’s a one-off script that is used to process data that came in that form, it’s not a big deal. I’m just stressing this point because people often just assume some “facts” without taking into account all the factors or doing some measurements, when warranted.
No, I agree it wasn’t helpful, but I wasn’t really serious when I posted that. It was about to go to sleep and the OP put some extra constraints on the solution (put the lines in an array, why?) so I didn’t feel like explaining bash arrays or asking why it had to be in an array. I figured someone else would come along anyway.
if you are not convinced ,you can try creating a big file in terms of MB, time the process using bash while read loop, vs one with only awk and see for yourself.