LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script for displaying a file one line at a time. (http://www.linuxquestions.org/questions/programming-9/bash-script-for-displaying-a-file-one-line-at-a-time-491220/)

panchosansa 10-10-2006 04:25 PM

bash script for displaying a file one line at a time.
 
Hello,

I'm trying to implement a script that lists a file one line at a time as a part of an experiment (I want to more or less duplicate the behavior of the cat function using other bash commands). Why would I want to do this? I finished my first script a couple of days ago and I just want to try something a bit more different. So here is what I have now:

Code:

line=1
last_line=$(tail -1 < whatever_file)
line_to_display=$(head -$line < whatever_file | tail -1)
echo "$line $line_to_display"
let "line+=1"

This, of course, should have the same result as cat -n whatever_file. It works fine but I'm wondering are there any shorter or simpler workarounds. I would be grateful if anyone bothers to outline or code his idea. I would really appreciate seeing the approach of someone more experienced than me.

unSpawn 10-10-2006 05:09 PM

This, of course, should have the same result as cat -n whatever_file.
All "cat -n" does is add a line number, so:
Code:

nl whatever_file
or
Code:

n=0; cat whatever_file|while read l; do ((n++)); echo "$n ${l}"; done
Note in your example you can write
Code:

let "line+=1"
as
Code:

line=$[${line}+1]
or
Code:

((line++))
and the
Code:

line_to_display=$(head -$line < whatever_file | tail -1)
could be something based on sed:
Code:

((LINENO++))
sed -n "$LINENO,1p" whatever_file

Just thinking out loud...

function help() {
echo "${FUNCNAME}: Bash scripting guides
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/"
}

panchosansa 10-10-2006 05:19 PM

Thanks once again unSpawn. I can only wish that some day I would be able to think out loud in the same way :). I will try your suggestions and then post the whole code for mycat function. I guess that for most of the things there will be a much simpler way.

P.S. I've gone through the Bash-Beginner-Guide and it's great reference.

panchosansa 10-10-2006 06:31 PM

I'm through with some of stuff and I think I will be satisfied if this works because it incorporates the most common cat properties.

Code:

#!/bin/bash
arguments=$#

if [[ "$arguments" -eq 0 ]]
then
 while [[ "$input" != "EndInput" ]]
 do
 read input
 echo $input
 done
else
 case $1 in
  -n)
  nl < $2  #do I have to redirect this?
  done;;

  -E)
  sed 's/$/$/' < $2 #puts $ in the end of each line ???
  done;;

  -h)
echo "mycat. Concatenates and/or displays files from standard input to standard output for free."
echo "SYNOPSIS"
echo "\t mycat.sh [OPTIONS] [FILE]"
echo "OPTIONS"
echo "\t -n \t number all output lines"
echo "\t -E \t displays $ at the end of each line"
echo "\t -h \t display help"

  *) #case with more than one file.
  while [ "$arguments" -ne 0 ]
  do
  line_number=1
  last_line=$(tail -1 < $1)
  until [[ "$last_line" == "$line_to_display" ]]
  do
  last_line=$(tail -1 < $1)
  line_to_display=$(head -$line_number < $1 | tail -1)
  echo "$line_to_display"
  ((line_number++))
  done
  shift
  arguments=$#
  done;;
 esac
fi
exit 0

I would be really grateful to anyone who can run the script and tell me if its sth like the actual cat command because I've uninstalled my Mandriva today and haven't been able to reinstall it yet. I would really appreciate remarks as to how to simplify things (especially the last case) and other general stuff that you think is important.

I do hope I am not asking too much and bothering you with my stuff.

Kindest regards,
Valentin

ghostdog74 10-10-2006 06:50 PM

Quote:

Originally Posted by panchosansa
Hello,

I'm trying to implement a script that lists a file one line at a time as a part of an experiment (I want to more or less duplicate the behavior of the cat function using other bash commands). Why would I want to do this? I finished my first script a couple of days ago and I just want to try something a bit more different. So here is what I have now:

Code:

line=1
last_line=$(tail -1 < whatever_file)
line_to_display=$(head -$line < whatever_file | tail -1)
echo "$line $line_to_display"
let "line+=1"

This, of course, should have the same result as cat -n whatever_file. It works fine but I'm wondering are there any shorter or simpler workarounds. I would be grateful if anyone bothers to outline or code his idea. I would really appreciate seeing the approach of someone more experienced than me.

If you have Python:
Code:

## -n case
for linenum, line in enumerate(open("file.txt")):
    print "Line number: %d" %(linenum)
    print "Line content: %s" %(line)


konsolebox 10-11-2006 07:51 AM

hello there. perhaps you're after something like this...
Code:

n=0

IFS=$'\n'
for a in $(<whatever_file); do
  (( ++n ))
  : statements here
done
IFS=$' \t\n'


panchosansa 10-12-2006 10:54 AM

I've resolved my problems after re-installing Mandriva and now everything is fine and the script is working. Thaks for your suggestions and help, everyone. I will be posting the source when I come back home.


All times are GMT -5. The time now is 04:51 AM.