LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2008, 02:31 AM   #1
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Rep: Reputation: 15
Thumbs up 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?
 
Old 03-11-2008, 02:38 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Check this out:

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

Then look up re-direction.
 
Old 03-11-2008, 11:27 PM   #3
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Smile

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
 
Old 03-12-2008, 12:30 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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'
 
Old 03-12-2008, 01:57 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 11:40 AM
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 07:29 AM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
Script to read a simple text file ravykanth Linux - Newbie 9 10-22-2003 04:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration