InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Taking It One Character at a Time in bash
Bash

Taking It One Character at a Time in bash

InfinityCoder February 21, 2017

You have some parsing to do and for whatever reason nothing else will do—you need to take your strings apart one character at a time.

The substring function for variables will let you take things apart and another feature tells you how long a string is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash
# cookbook filename: onebyone
#
# parsing input one character at a time
 
while read ALINE
do
 
  for ((i=0; i < ${#ALINE}; i++))
  do
      ACHAR=${ALINE:i:1}
      # do something here, e.g. echo $ACHAR
  done
done

The read will take input from standard in and put it, a line at a time, into the variable $ALINE.

Since there are no other variables on the read command, it takes the entire line and doesn’t divvy it up.
The for loop will loop once for each character in the $ALINE variable. We can compute how many times to loop by using ${#ALINE}, which returns the length of the contents of $ALINE.
Each time through the loop we assign ACHAR the value of the one-character substring of ALINE that begins at the ith position.

That’s simple enough. Now, what was it you needed to parse this way?

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Reusing Code with Includes and Sourcing in bash
There are a set of shell variable assignments that you …

Reusing Code with Includes and Sourcing in bash

Searching for an SSN in bash
You need a regular expression to match a Social Security …

Searching for an SSN in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap