Read paragraph from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read paragraph from file
# 1  
Old 07-08-2015
Read paragraph from file

Hi,

I have a file containing SQL commands in following format. I need to run the SQLs separately and also print the status of SQL, successful/unsuccessful.

File : SQL.dat
Code:
## SQL1
select * from dual;

## SQL2
select user from dual10;

Expected output:
Code:
SQL1:PASS
SQL2:FAIL

Started with kind of a FOR loop (as mentioned below). But this prints the entire SQLs in file. I want SQL1 to be executed then SQL2 and so on. Please suggest.

Code:
for Code in $(cat SQL.dat | egrep "^##")
do
    SQLID=`echo "$Code" | tr -d "##"`
    SQL=`cat SQL.dat | sed -n "/$Code/,/;/p"`
    RunSQL $SQL
done

Thanks for your time.
# 2  
Old 07-08-2015
Don't use for to read lines. Use a while read loop.

This will loop over the file and grab the statement name from your ## lines. probably only good for single-line statements

Code:
#!/bin/bash

while read -r sql; do
  if [[ $sql = \#* ]]; then
    stmt=${sql#* }
  elif [[ $sql = *[![:space:]]* ]]; then  # has something besides blank
    if RunSQL "$sql"; then
      echo "$stmt:PASS"
    else
      echo "$stmt:FAIL"
    fi
  fi
done < "SQL.dat"


Last edited by neutronscott; 07-08-2015 at 12:06 PM..
# 3  
Old 07-09-2015
Thanks for your reply.

But the code suggested does grep for SQL commands if spanned across multiple lines.
# 4  
Old 07-09-2015
You can try this adaption of neutronscott's proposal, but be aware it needs an empty line at file end:
Code:
while read -r TMP
  do if [[ $TMP = \#* ]]
       then     stmt=${TMP#* }
       elif [[ $TMP = *[![:space:]]* ]]
         then sql="$sql $TMP"
         else
           if RunSQL "$sql"
             then       echo "$stmt:PASS"
             else       echo "$stmt:FAIL"
           fi
         sql="";
       fi
  done < file

If that can't be taken for granted, you'll need to add the entire RunSQL paragraph again at script end. Or you could go for the terminating semicolon.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join Lines every paragraph in a file.txt

Hi all, Is there any idea on how to automate convert the paragraph in one line in a file, this will happen after OCR the documents, OCR split every paragraph. I need to join all the paragraph in one line. #cat file.txtThe Commission on Higher Education (CHED) was created through Republic Act... (7 Replies)
Discussion started by: lxdorney
7 Replies

2. Shell Programming and Scripting

Retrieving a paragraph from a pdf file using shell commands

In the reference section of a research paper(in pdf form), many other paper names are cited which have been used inside the pdf at different places. If I give an input, the name of a paper which has been cited in the reference section and want to display the section (the paragraph) inside the pdf... (1 Reply)
Discussion started by: SK33
1 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Shell Programming and Scripting

Print each paragraph in a text file into excel file

Hi, I have a below text file, which needs to be printed into attached excel file. Please help me. Thanks (4 Replies)
Discussion started by: prash358
4 Replies

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

Easy way to pull a paragraph from a text file?

I have a collection of text files that comprise a mailing list archive. I grep them to find an email that interests me, then open the file in a text editor to find the surrounding paragraph of text. Is there an easy way to do this from the shell instead? (2 Replies)
Discussion started by: CRGreathouse
2 Replies

7. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

8. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. Shell Programming and Scripting

Replacing a paragraph between pattern , with the content 4m another file

hi, i wanted to put the output of file f1 into the pattern space of file f2 f1: wjwjwjwjwjwjwj //these line go in file f2 jwjwjwjwjwjjwjw wjwjwjwjjwjwjwj f2: Pattern_start __________ //these are the line to be replaced __________ Pattern_end i m... (4 Replies)
Discussion started by: go4desperado
4 Replies

10. Shell Programming and Scripting

selecting each paragraph and put it into a file...help me

Dear Friends, I need a shell script. I am facing a problem while selecting the text that is between start and end tags. and insert the paragraph into a file1, next paragraph in file2...... experts please help. i have a file which contains, -------------- <abc> 111some text some text some... (2 Replies)
Discussion started by: swamymns
2 Replies
Login or Register to Ask a Question