LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to read text file using bash script (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-read-text-file-using-bash-script-627146/)

kkpal 03-11-2008 03:31 AM

how to read text file using bash script
 
hi

Let suppose i have a text file test.txt
It contains following lines:
komal
database45
classroom
45822664

I want to read above file using bash script and store in variables.
like..
name="komal"
type="database"
loc="classroom"
snum="45822664"


how can I do it?

matthewg42 03-11-2008 03:38 AM

Check this out:

http://tldp.org/LDP/abs/html/internal.html#EX36

Then look up re-direction.

kkpal 03-12-2008 12:27 AM

hi all

solution is this:


name=`head -n1 test.txt |tail -1`
type=`head -n2 test.txt|tail -1`
loc=`head -n3 test.txt|tail -1`
snum=`head -n4 test.txt|tail -1`


Thanks & Regards
KKPal

matthewg42 03-12-2008 01:30 AM

kkpal, your solution will work, but it is inefficient - it invokes 8 external processes. Of course most of the time, performance is not an issue, else the programmer would not be using shell script, but even so, it's a good idea to try to reduce the number of external processes which are invoked.

A slightly less readable, but more efficient way to do it:

Code:

#!/bin/bash

eval $(sed -e '1s/^/name=/' -e '2s/^/type=/' -e '3s/^/loc=/' -e '4s/^/snum=/' test.txt)

To understand what is going on, you might run this command from the prompt:
Code:

sed -e '1s/^/name=/' -e '2s/^/type=/' -e '3s/^/loc=/' -e '4s/^/snum=/' test.txt
...which will output this:
Code:

name=komal
type=database45
loc=classroom
snum=45822664

The eval just takes this output as a string and executes it in the current shell. There are a few implications of this:
  • For real world applications, you must be very careful what is in the data. Say this data comes from a web form... Some attacker might put shell commands web form and be able to have your program execute them! Imagine some malicious command like destroying your files, or worse, downloading and installing a root kit. An attacker might inser it by having a specially crafted name field, like this:
    Code:

    evil$(evil command here)haxor
    Generally speaking you should not eval data coming from outside your code unless you are take a lot of care to prevent this sort of thing. The same applies when you are taking external data and using it to build SQL statements.
  • Imagine one of the lines in the file containing a space. Say the name (first line) is "Matthew Gates". In your eval'd code this will end up as:
    Code:

    name=Matthew Gates
    This is no good - if you want to assign the whole string including the space and everything after it, you must quote the data. This can afford some protection from embedded code attacks too, but you must not leave the attacker the option to prematurely end your quote. For example, the sed command might become:
    Code:

    sed -e "s/'//g" -e "s/^/'/" -e "s/$/'/" -e '1s/^/name=/' -e '2s/^/type=/' -e '3s/^/loc=/' -e '4s/^/snum=/' test.txt
    Here we add three new sed commands. The first one removes existing quotes from the input data, the second and third add single quotes to the beginning and end of all lines. Now the assignment of the first line looks like this:
    Code:

    name='Matthew Gates'

jschiwal 03-12-2008 02:57 AM

Are there more records than the one?
Code:

komal
database
classroom
45822664

smith
art15
classroom
45823333

If so then awk is a good candidate for handling them. The only difficulty is using quotes around the names since quotes are used in the sed command.

Code:

> awk 'BEGIN { RS=""; FS="\n" } {print "name=\""$1"\"\ntype=\""$2"\"\nloc=\""$3"\"\nsnum=\""$4"\"\n"}' sample
name="komal"
type="database"
loc="classroom"
snum="45822664"

name="smith"
type="art15"
loc="classroom"
snum="45823333"



All times are GMT -5. The time now is 09:27 AM.