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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: Reading out rows of a file into a dynamic array and check first literal
# 8  
Old 08-01-2009
I still don't think you need an array:

Code:
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:

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

# 9  
Old 08-02-2009
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 '*'
# 10  
Old 08-02-2009
Quote:
Originally Posted by ABE2202
[...]
The code is unfortunately not enough, because the string MOVIE should ONLY occur with a '*' as first literal.
Could you post a sample data and elaborate further?

Quote:
You did not check the case when it starts with an empty space for instance.
Yes, that was an example.
It's quite easy to modify the regular expression to match such a pattern.

Quote:
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 '*'
As always, sample data and expected behavior (based on the sample data) will be useful.

Last edited by radoulov; 08-02-2009 at 11:46 AM..
# 11  
Old 08-03-2009
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

Code:
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 '*' "

Code:
grep not ^* Output1 >Output2

That does not work of course, but how can I write a NOT-Statement for the first literal?
# 12  
Old 08-04-2009
If I understand correctly:

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

# 13  
Old 08-04-2009
this is already solved. im not sure what's the problem though.. perhaps you missed to escape *?
Code:
grep ^MOVIE MySampleInfile >Output1
grep ^\* MySampleInfile >Output2

# 14  
Old 08-04-2009
I solved it without array and like this:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Check for empty line at end of a dynamic header in each file

Hi Folks, I have a requirement to develop a shell script. PFB my requirement, Requirement: I need to check an empty line after the end of each header in respective file and if a empty line is present simply echo file OK and if empty line is not present echo "Adding empty line" and add an... (6 Replies)
Discussion started by: tpk
6 Replies

2. Shell Programming and Scripting

Reading a long literal continued next line

I am trying to identify all messages or prompts from a number of COBOL programs and they can usually be identified by a pair of double quotes on one line. However, sometimes the literal will not be finished on the first line but after a dash in column 7 of the next line, the literal will be... (6 Replies)
Discussion started by: wbport
6 Replies

3. Shell Programming and Scripting

Help w/ Reading Matrix & Storing in dynamic array

First of I would just like to state that I am not looking for you guys to just do my work for me, I do want to learn and actually understand everything that is happening. Hey all, I am having trouble on this. What I need to do is... Write an executable C file that will take a text file (not a... (8 Replies)
Discussion started by: innvert
8 Replies

4. Shell Programming and Scripting

Reading a file into an array

Hi I have a file with contents as below : server | ABC Issue : File System Missing XYZ Issue : Wrong Syntax PQR Issue : Old File to be removed Now I am looking for an o/p similar to server <tab> ABC Issue : File System Missing <tab> XYZ Issue : Wrong Syntax <tab>... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

5. Shell Programming and Scripting

Reading a file into array

Hi, I need to read a file into array and print them in a loop:- 1st file :-cat a.txt RC1 RC2 RC3 RC4 My Program:- #!/bin/ksh index=0 while do read cnt<a.txt print "cnt value is ${cnt} index=`expr $index + 1` done Code tags for code, please. (5 Replies)
Discussion started by: satishmallidi
5 Replies

6. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

7. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

8. UNIX for Dummies Questions & Answers

Reading a file into an array

I have a file that is a text file, how to get all the words into and array, i am able to get each line but not each word :(. Here is what i searched and already found...https://www.unix.com/shell-programming-scripting/99207-pipe-text-file-into-array.html. This one reads a whole line into... (6 Replies)
Discussion started by: SasankaBITS
6 Replies

9. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

10. Shell Programming and Scripting

Is there any commands to check the dynamic changes of a file

Hi guys i had a script which will generate a log file.Is there any commands to check the dynamic changes in the log file,i.e if i open the log file i should able to see the updating changes live...I hope u understand my query... (2 Replies)
Discussion started by: vinoo
2 Replies
Login or Register to Ask a Question