About Linux FAQ

Browse More FAQs:

Howto Display text files in a shell script

Posted by Vivek Gite [Last updated: May 2, 2007]

Q. How do I display a text file stored in /tmp directory in a shell script?

A. There are many ways, you can simply use cat command and display back output on screen or read file line by line and display back the output. In some cases you may need to store output to a variable and later display back on screen.

Task: Display output using cat command

Following sample script displays contents of a file called /tmp/input.txt:

#!/bin/bash
FILE="/tmp/input.txt"
echo "*** File - $FILE contents ***"
cat $FILE

Task: Store a file output in a variable

Following sample script displays nameserver IPs:

#!/bin/bash
FILE="/etc/resolv.conf"
OUT=$(awk '{ print $2 }' $FILE)
echo "*** Nameserver IP ***"
for ip in $OUT
do
   echo $ip
done

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. shekar Says:

    2nd option is good. If file is large, one can modify the script a bit to serve the purpose.

  2. Palash Khan R. Says:

    How to display certain part of a file? For example, I have a file myFile.Java and I would like to display the contents between lines 30-40. How to do that in linux? with cat?

    Thanks,

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.