
How do I read lines of data in a shell script?Dave, where can I find a bash script that can read data from a file; the information should be separated by tabs or commas for easy pickup, and can only be accesed by a row. This sounds suspiciously like a homework assignment, something I generally don't offer assistance with since I think students should do their own work, but this particular question appears in my mailbox often enough that I thought it would be valuable to address it here. There's a very easy way to solve this: while read myline
do echo $myline done < inputfile If the fields in a given line are separated by a known delimiter, either a tab or a comma, for example, then I suggest that you could use the cut command to extract specific values. To demonstrate, let's pull some useful data out of the /etc/passwd file, a file that has lines of data in known fields, separated with a ":" as the deilmiter. Here's a typical line of data: unknown:*:99:99:Unknown User:/var/empty:/usr/bin/false
The first field (remember, they're separated by colons) is the account name, the second the encrypted password (not shown because it's in a separate 'shadow' file for security), then the remaining fields are account ID, group ID, full user name, home directory and login shell. Let's just pull out login and full name to see what that looks like: #!/bin/sh
while read inputline do login="$(echo $inputline | cut -d: -f1)" fulln="$(echo $inputline | cut -d: -f4)" echo login = $login and fullname = $fulln done < /etc/passwd exit 0 You can see how the cut program makes this a straightforward task, albeit one that can be done more quickly in other scripting languages like Perl. But if you want to work with shell scripts, the combination of a while read loop with the input redirected and the great cut command should give you all the data parsing capabilities you need. Hope that helps you out with your homework. :-)
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
(Article 4282)
Tagged: Previous: How do I read a Google AdSense report? Next: Automating SSH with a shell script Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. how can i read an input stream i.e. if piped from another command into my shell script ? Posted by: me at December 14, 2005 5:29 PMThe great thing about Unix is that your shell script basically can't differentiate between you typing in lines directly and that input coming from a file redirect or even a pipeline of commands. So just write your script to read "stdin", as I show above, and you'll be good to go! Posted by: Dave Taylor at December 14, 2005 11:07 PMYou know, there's more than just students out there looking for relatively basic scripting questions. I am not a student but am a Linux amateur, and spent probably 45 minutes figuring out how to make my more sophisticated shell scripts read from configuration files (to increase portability and share value of the scripts), before I finally found your tip. At first I used piping (i.e. cat myfile | while read myvar), but the variables don't survive when the pipe is done. Your answer was just what I needed. Thanks. Posted by: Joshua Curtiss at January 27, 2007 4:49 PMHow should I change the script if in case the file to be read is remotely located ? I want to read a DNS server password file which is located on my PC . Posted by: Pooja at February 27, 2007 10:26 PMDave, excellent stuff, thanks. I've been spending days trying to figure out how to use a configuration file and break it down like this - your script is just what I was after. I have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|