Thread: how to open files and print line by line in shell

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    29

    how to open files and print line by line in shell

    i am using this
    Code:
    #!/usr/bin/env bash
    
    curdir=`pwd`
    while read line; do
    	printf $line
    done < "test"
    cd $curdir
    but this is only printing the strings that end with \n, and skipping the last one because it doesnt have it. How can I print them all?

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I'm not catching the part where you tell the script what to read. It looks like it reads from the standard input.

    Also, why in the world do you change to your current directory? (cd `pwd`)

    According to my man page, 'read' can be given a fd, and will return on eof, so making it do what you want should be very possible with 'read'. Check out the page for your self, it could be of some help.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    This forum is for C programming questions specific to Linux. You would be better off going to a Linux specific forum, but I do have something of an answer for you.

    You need to check the return value of read, to see if it's zero (read a normal line, with a newline at the end) or non-zero (something else, as Yarin pointed out). The solution I came up with is:
    Code:
    x=0 # used to track the return value of read
    while [ $x == 0 ]; do # bash has no do-while, this will stop only when x is non-zero, i.e. read gets an EOF
        read line # read a line, up to a \n
        x=$? # set x to the return value of the last command
        printf $line
    done < foo.txt
    Note, you can't specify "test" as you did with the quotes, since that's a string, not a file name, and < redirect doesn't work with strings. You would have to use echo "test" | .... Also, if some of the files you process do have a newline at the end of the file, you may need to deal with the empty line it reads since it saw the EOF. You can do something like if $line is not empty or $x is zero, print $line.

    I'm not a bash expert, so there may be a way sexier way. Two bash references I like are Deadman.org: Advancing in the Bash Shell and BASH Cures Cancer.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by anduril462 View Post
    I'm not a bash expert, so there may be a way sexier way. Two bash references I like are Deadman.org: Advancing in the Bash Shell and BASH Cures Cancer.
    The "sexy" way is how he's doing it. It should be working. I suspect the reason it's not working is because the last line has no newline, and though it DOES get printed, it doesn't flush to the screen right away. If you just put a blank "echo" right before the "cd $curdir" what happens?

    Code:
    #!/usr/bin/env bash
    
    curdir=`pwd`
    while read line; do
    	printf $line
    done < "test"
    echo
    cd $curdir
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yeah, bash read it, but unfortunately, read got an EOF, so it skipped the while loop that would print it. Since the data isn't in the output buffer, an echo wouldn't flush anything. You're right he is doing it the better way, but the solution is not a plain echo, but to echo $line after the loop. I think this should take care of any file, whether or not it has the trailing end of line:
    Code:
    while read line; do
        printf $line
    done < "test"
    echo -n $line
    The echo -n doesn't print the trailing new line, so it's more like printf in that regard. I would just use printf, but it complains about an empty parameter list if $line is empty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. How to have fgets() return edited line like in shell?
    By hkluis in forum Linux Programming
    Replies: 3
    Last Post: 10-14-2006, 01:46 AM
  4. Replies: 1
    Last Post: 05-20-2006, 11:17 PM
  5. print line by line from a file
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 01:36 PM