LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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


Closed Thread
  Search this Thread
Old 08-26-2006, 02:55 PM   #1
ozymandias
Member
 
Registered: Aug 2005
Location: West Midlands, UK
Posts: 61

Rep: Reputation: 15
How to read data from file to use in shell script?


I'm trying to create a shell script that makes use of a data file with text on individual lines. I want the script to read the first line of the data file, then the second line etc. etc.
I can't seem to find out which command to use - sorry if this is a simple one!
I have tried a few man pages but if you don't know which command you're looking for it's difficult to work backwards!
Cheers,

Oz
 
Old 08-26-2006, 03:28 PM   #2
jp-lack
Member
 
Registered: Mar 2005
Location: NJ - US
Distribution: Slackware
Posts: 93

Rep: Reputation: 15
just to read the content of the file and echo them

Code:
#!/bin/sh
var=`cat file`
for i in $var; do
        echo $i
done
of course that you should verify if the file exist and bla bla bla
 
Old 08-26-2006, 03:33 PM   #3
ozymandias
Member
 
Registered: Aug 2005
Location: West Midlands, UK
Posts: 61

Original Poster
Rep: Reputation: 15
aha! Thanks
 
Old 10-27-2006, 11:05 AM   #4
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Rep: Reputation: 30
may I hijack the thread and ask how to make it seperate the lines instead of the words? The above code will print every word and not sentences, so a file containing,

this is a test line
this is another test line

will return:
this
is
a
test
line
this
is
another
test
line

when I want a specified sentence (like line 2) to be placed in a variable.


Edit: I thought I knew SED pretty well, but I had never knew you could do it there. Here is an example if someone else needs it. LINE2="`sed -n '2p'file-location`"

Last edited by Fredde87; 10-27-2006 at 11:16 AM.
 
Old 10-27-2006, 12:47 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
If I remember correctly, awk reads one line at a time.
 
Old 10-27-2006, 12:58 PM   #6
Dragineez
Member
 
Registered: Oct 2005
Location: Annapolis
Distribution: Ubuntu
Posts: 278

Rep: Reputation: 41
Correct

Quote:
Originally Posted by pixellany
If I remember correctly, awk reads one line at a time.
You beat me to it!
 
Old 10-27-2006, 01:04 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Fredde87
Edit: I thought I knew SED pretty well, but I had never knew you could do it there. Here is an example if someone else needs it. LINE2="`sed -n '2p'file-location`"
Need a space before file
The quotes are not needed, since the "`" makes a container

Thus: LINE2=`sed -n '2p' file-location`
Yes, sed is cool....
 
Old 10-27-2006, 01:19 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Dragineez
You beat me to it!
My motorcycle is faster...
 
  


Closed Thread

Tags
data, script



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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM

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

All times are GMT -5. The time now is 03:57 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