Question : Bash Scripting - Read by lines

Hello All,

I have a file that is exported from SQL in TXT format, the file has the following:

( I will call this file, SQLFile)
CustomerName
info1
info2
info3

now I need to make script to read the info, and add it to a different file ( I will call this file LinuxFile), here is challenge:

not all the SQLFiles has the same amount of lines, but all the files has a least 2 lines ( customername and info1), so, I am looking to make an script to read the SQLFile line by line but the script needs to know how many lines he will read, becuase he will need to stop on a blank line, on the SQLFile example, the file has 4 lines, so, the script will stop reading line after line 5, but he know the line 4 is the last line of the file, then from the same script o I dont know from where, I will need to import those values on something like this:

"CustomerName"me>
blahblahblah
blahblahblah
blahblahblah
blahblahblah
"Info1 goes here"
blahblahblah
blahblahblah
"Info2 goes here"
"Info3 goes here"

if you see, there is not order on how or where the values will be inserted, does any one can help me out with this?

thanks!!!!

Manny

Answer : Bash Scripting - Read by lines

You said "but the script needs to know how many lines he will read, becuase he will need to stop on a blank line" so I thought it could be a single file. It wasn't very clear. So there are no blank lines, just a lot of little files with 2 or more lines in?

All variables in bash are global and don't need predeclaring, so you can use $INFO2 and if it wasn't already set in a previous run further down the loop it doesn't matter. If it was previously defined then it will contain the value.

OUTFILE in my example is supposed to be the path to your LINUXFile; that part was writing out the lines to your LINUXFile having read them in from the SQLFile

Anyway try the following, between the two scripts you should be able to work out all you need. The key thing is catting the file and piping the result to "while read line"; then inside the while loop you can access $line which will contain the current line contents

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
 
# assuming ".ext" extension
 
for file in *.ext
do
  cat $file | while read line
  do
    if [ "$CUST" = "" ]
    then
      CUST=$line 
    fi
 
    if [ "$INFO1" = "" ]
    then
      INFO1=$line 
    fi
 
    # repeat the following as many times as you need
    # for INFO3, INFO4 etc
    if [ "$INFO2" = "" ]
    then
      INFO2=$line 
    fi
 
  done
 
  # vars have been read for this file, now for output.
  # if you need to output all to one file, define it above
  # outside the loops as OUTFILE=/path/to/outfile
  # 
  # the following shows an example of outputting a file with
  # the same name as the original but with extension ".xml"
  # instead of ".ext"
 
  # comment this line out if you have an OUTFILE defined above
  OUTFILE=`echo $file | sed "s/ext$/xml/"`
 
  echo "$CUST" >> $OUTFILE
  echo "$INFO1" >> $OUTFILE
  
  # repeat the following as many times as you need
  # for INFO3, INFO4 etc
  if [ "$INFO2" != "" ]
  then
    echo "$INFO2" >> $OUTFILE
  fi
 
done
Open in New Window Select All
Random Solutions  
 
programming4us programming4us