≡ Menu

Shell Script Utility To Read a File Line By Line

  1. #!/bin/bash
  2. # Shell script utility to read a file line line.
  3. # Once line is read it can be process in processLine() function
  4. # You can call script as follows, to read myfile.txt:
  5. # ./readline myfile.txt
  6. # Following example will read line from standard input device aka keyboard:
  7. # ./readline
  8. # -----------------------------------------------
  9. # Copyright (c) 2005 nixCraft <http://cyberciti.biz/fb/>
  10. # This script is licensed under GNU GPL version 2.0 or above
  11. # -------------------------------------------------------------------------
  12. # This script is part of nixCraft shell script collection (NSSC)
  13. # Visit http://bash.cyberciti.biz/ for more information.
  14. # -------------------------------------------------------------------------
  16. # User define Function (UDF)
  17. processLine(){
  18. line="$@" # get all args
  19. # just echo them, but you may need to customize it according to your need
  20. # for example, F1 will store first field of $line, see readline2 script
  21. # for more examples
  22. # F1=$(echo $line | awk '{ print $1 }')
  23. echo $line
  24. }
  26. ### Main script stars here ###
  27. # Store file name
  28. FILE=""
  30. # Make sure we get file name as command line argument
  31. # Else read it from standard input device
  32. if [ "$1" == "" ]; then
  33. FILE="/dev/stdin"
  34. else
  35. FILE="$1"
  36. # make sure file exist and readable
  37. if [ ! -f $FILE ]; then
  38. echo "$FILE : does not exists"
  39. exit 1
  40. elif [ ! -r $FILE ]; then
  41. echo "$FILE: can not read"
  42. exit 2
  43. fi
  44. fi
  45. # read $FILE using the file descriptors
  47. # Set loop separator to end of line
  49. IFS=$(echo -en "\n\b")
  50. exec 3<&0
  51. exec 0<"$FILE"
  52. while read -r line
  53. do
  54. # use $line variable to process line in processLine() function
  55. processLine $line
  56. done
  57. exec 0<&3
  59. # restore $IFS which was used to determine what the field separators are
  61. exit 0

Comments on this entry are closed.

  • http://divinequran.com ravi

    for i in ‘input filename’
    echo $i

    This code will print file line by line.

  • lost

    Thanks, I used this code and edited it. It’s the closest I’ve been to the output I need but for some reason using this to export each line as an individual variable isn’t working.

    Here is the script, my guess is that it’s a syntax issue of some sort. Thanks in advance for anyone who offers help! http://pastebin.com/m3472cd27

  • Vivek Gite

    Can you tell me what are you trying to create here? If possible please use our forum for further discussion.

  • http://javaworks.wordpress.com Hari

    I have used your example countless time when i wanted to write a quick hack. Thank You!

  • nishant

    that bloody “head” never came to my head.

  • http://online.promoters.ro ijo


  • Thushara

    Thank you for the post!!

  • vinu

    This is not working for me.

    sh read.sh v1.txt
    : not found:
    : not found: }
    : not found:
    : not found:
    read.sh: 40: Syntax error: “elif” unexpected (expecting “then”)

  • Sanjay

    Thank you for the post.
    It’s work perfectly but i need some changes
    can u help me?
    i want a search a specific pattern in a file and copy a a search result to xml file
    Thanks in advance.

  • Samuel

    Thanks for posting it!!!

  • sj

    I edited the code to input the contents of $line when ever the loop runs. it inputs line by line in a file that contains a list of id’s I pulled out of etc/passwd. for some reason it’s stops at the 25th line. it starts combining the variables to I dont’ know what’s going on. how it could work for the first 25 entries then break.

  • Vimarsh


    I need to read a file and see for the occurence /local/svncheckout/* at the begining of each line. If this instance is found I need to delete that particular line.

    I need to write this program using shell script (bash shell)

    Thanks a lot

  • http://NA SK

    Hi Very informative post. Thanks for this.
    But I have one confussion that
    what following code is doing. Please healp.
    IFS=$(echo -en “\n\b”)
    exec 3<&0
    exec 0<"$FILE"

  • http://www.cracktheinterview.org/ CrackTheInterview

    for line in `cat abc.txt`;do
    touch path/$line

    this 2 line is code enuff


    hi i want to update a port number in a file such like……
    file content…..abc.cfg

    # Temporary use by Boyajian. Thank you.
    # Plz feel free to change as per ur need. Thnx – Ayan



    now if i want to update the value of ACE_MATCH_SERVER_PORT from 16711 to 78777
    what would be the code to do this stub? please give a solution at the earliest

  • Dedeepthi

    You can try this simple code to read the file “file.txt” line-by-ine….

    while read line
    echo $line;
    sleep 1;
    done < file.txt

  • http://anon.com anon

    Don’t listen that fool Dedeepthi ↑
    Here is the real code:
    —–cut here—————
    for i in ‘cat file.txt’
    echo “$i”
    —–cut here—————

  • cavin

    THANK YOU for sharing the script..it’s very generous/kind sharing…THUMBS UP

  • Azaz

    Cool, I found this script very useful. I modified this and used to make our syncronisation across servers fully automated. Great Job