LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-10-2006, 04:25 PM   #1
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Rep: Reputation: 0
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.
 
Old 10-10-2006, 05:09 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,310
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
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/"
}
 
Old 10-10-2006, 05:19 PM   #3
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Original Poster
Rep: Reputation: 0
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.
 
Old 10-10-2006, 06:31 PM   #4
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Original Poster
Rep: Reputation: 0
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
 
Old 10-10-2006, 06:50 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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)
 
Old 10-11-2006, 07:51 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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'
 
Old 10-12-2006, 10:54 AM   #7
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
Get file modification date/time in Bash script cmfarley19 Programming 12 01-19-2013 09:37 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM
Simple bash problem - checking for files and displaying the first line morrolan Programming 4 03-07-2005 10:40 AM
Displaying a certain line in a file. Mike_the_Man Linux - General 1 03-02-2001 10:12 PM


All times are GMT -5. The time now is 05:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration