Bash scripting: parsing a text file character-by-character
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 scripting: parsing a text file character-by-character
Hi all,
Is there a way to process individual characters one-by-one from a text file in Bash, or is that hoping for a little too much from this lovable old clunker?
~$ help read
read: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
...
If -n is supplied with a non-zero NCHARS argument, read returns after NCHARS
characters have been read.
So ya seein' the light, huh? Praise be and pass the pipe!
Hello Charles,
Well I'm simply more realistic about Bash's capabilities now I know how it arose and what for.
This task is something I could jot down on the back of a fag packet in C, but that was very many years ago and I can't recall the relevant functions. ISTR C has a getchar() function which does this. I need a Bash equivalent that I can run in an 'until' loop until the next character read from the file is EOF.
It's annoying that so much I want to do in Linux is usually something I've yet to learn!
This task is something I could jot down on the back of a fag packet in C, but that was very many years ago and I can't recall the relevant functions. ISTR C has a getchar() function which does this. I need a Bash equivalent that I can run in an 'until' loop until the next character read from the file is EOF.
It's annoying that so much I want to do in Linux is usually something I've yet to learn!
Ah, the delights of steep learning curves!
Solutions already posted show how to do what you want in bash; if you want to do a non-blocking read -- simple in C -- then things get a little more "interesting"
ISTR that getchar() and putchar() are actually C #define macros that hide an underlying buffer for convenience and performance but that also comes from "very many years ago" so take it with a pinch of salt.
Okay, well we're off and running, but a few teething problems. This is the relevant code fragment as it stands after some tinkering:
Code:
#!/bin/bash
#process a text file character-by-character
# the variable '$char' is the individual file character under test
x=1
while read -n$x char
do
echo -n "iteration number $x:"
echo " $char"
x=$[ $x + 1 ]
done < textfile.txt
The problem is that I'm incrementing the -n of 'read' using the variable $x each time the loop iterates, but each time read functions, it's reading longer and longer strings from the file each time around. I only want it to fetch *one* character at a time to test on; kind of like a pointer moving up an array one element at a time.
How do I tell 'read' to just fetch one character each time, but every iteration make it take the *next* character and so on in sequence until the end of the file is reached?
Thanks, guys.
Is there a way to process individual characters one-by-one from a text file in Bash, or is that hoping for a little too much from this lovable old clunker?
ISTR that getchar() and putchar() are actually C #define macros that hide an underlying buffer for convenience and performance but that also comes from "very many years ago" so take it with a pinch of salt.
(taking the tangent...) It doesn't make a lot of sense to use an input buffer in that manner, anyway. In C you could read in a line of text, then process it a character at a time from an array. If the objective was to react to keyboard input immediately, the read system call would be more appropriate (after changing the terminal settings) with a requested size of 1. I think the objective of getchar is to avoid incrementing the buffer by an entire line, or to simulate read when one is predominantly using text input.
Kevin Barry
#!/bin/bash
#process a text file character-by-character
# the variable '$char' is the individual file character under test
count=1
while read -n1 char
do
echo -n "iteration number $count:"
echo " $char"
(( count++ ))
done < textfile.txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.