bash script for displaying a file one line at a time.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.
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.
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:
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.