Bash: Reading out rows of a file into a dynamic array and check first literal

Hello,

i have a file "Movie.ini" looking e.g. like follows

* MOVIE A
bla bla
MOVIE B
blubb blubb
MOVIE C

I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig.

By doing

"cat Movie.ini| grep MOVIE >> tmp_MOVIE " 

all rows containing MOVIE should be saved in a tempfile

* MOVIE A
MOVIE B
MOVIE C

I have following problem, since I just know a bit ksh but not bsh:

  1. How can I read out the rows of the file "tmp_MOVIE" in a VARIABLE long / dynamic allocated array? (Since there is NOT a fix number of rows containing the string MOVIE - it can be 3, 5 or e.g. 20)
  2. How can I check the first literal of an array-element whether it is a '*'?

Thanks a lot for any helpful answer

Please use CODE tags next time when displaying code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

I believe that one grep would be sufficient:

grep '^* MOVIE' Movie.ini

Hello Thanks,

I'll try it, but how can I handle assigning a dynamic number of stings into arrays?

Why do you think you need an array?

I want to read out ALL appearances of the string 'Movie' and check whether they ALL beginn with a '*'. So I think I need an array with a DYNAMIC number of fields since they number of 'Movie'-string varies.

grep will search for the pattern specified in it ( "^ * MOVIE" in your case ) in the whole file and whenever it finds, it will print the whole line.

so you don't need array.

man grep

for more details. also

man regex

But I think I need an array, because I want to check within a script if there is MOVIE without '' and if there is a MOVIE without '', a mail will be send. That part is already written and running, I just need a working if-clause.

I still don't think you need an array:

grep ^MOVIE infile >/dev/null &&
  printf '%s\n' "there is a MOVIE without *' |
    mailx -s ... email@address 

Notice that for some shells (like the Z-Shell) the ^ character is special and therefore you need to escape it.
Some implementations of grep support the -q/-s option to suppress the output, so if you use one of them, the command becomes:

grep -q ^MOVIE infile  &&
  printf '%s\n' "there is a MOVIE without *' |
    mailx -s ... email@address

Thank you for that code, I will try it.
Is there any good but short online manual for bash-shell?

---------- Post updated at 06:17 AM ---------- Previous update was at 04:51 AM ----------

The code is unfortunately not enough, because the string MOVIE should ONLY occur with a '' as first literal. You did not check the case when it starts with an empty space for instance. Acctually the empty space is not a problem as long as a '' follows, so let me state my problem elseway:
As long as the number occurances of the string MOVIE and '' is the same, everything is right. So grepping the lines containing MOVIE is just the first step, in addition I hav to check whether ALL that lines have ''

Could you post a sample data and elaborate further?

Yes, that was an example.
It's quite easy to modify the regular expression to match such a pattern.

As always, sample data and expected behavior (based on the sample data) will be useful.

Hi,
I need no array, but the postings so far do not solve my problem. Remember I need a file containing ALL lines from MySampleInfile containing the string 'MOVIE' but NOT beginning with '*'

I proceed now as follows, not needing an array anymore

grep MOVIE MySampleInfile >Output1

by this I have ALL lines containing Movie, no matter if they start with '*' or not

In a second step I want to filter all lines beginning with a space or another literal than ''. So may statement has to be "Filter ALL lines NOT beginning with '' "

grep not ^* Output1 >Output2

That does not work of course, but how can I write a NOT-Statement for the first literal?

If I understand correctly:

% cat infile 
* MOVIE A
bla bla
 MOVIE B
blubb blubb
xMOVIE C
% grep  '^[^*]*MOVIE' infile
 MOVIE B
xMOVIE C

this is already solved. im not sure what's the problem though.. perhaps you missed to escape *?

grep ^MOVIE MySampleInfile >Output1
grep ^\* MySampleInfile >Output2

I solved it without array and like this:

grep MOVIE $vINIFILE >>tmp_filter_1
grep ^[a-zA-Z0-9' '] tmp_filter_1 >>tmp_filter_2

So all lines NOT beginning with '*' are written into the file tmp_filter_2.
If the file tmp_filter_2 is NOT empty, I know for sure there is something wrong and I send my message.

that can be rewritten in reverse. ALL characters except *

grep -v ^\* tmp_filter_1 >>tmp_filter_2