darkwolf29a
darkwolf29a

Reputation: 1

Unix Character counting - Looking for a ksh solution

I have an existing ksh script that performs a number of different tasks.

I have been asked to add some more intelligence to the script.

So, I have been working to figure out how to add this:

In a file, on the first line, there is a tilde (~) character somewhere in the line. It is not always consistently in the same location. But, always three, 3, characters before that first tilde is a one letter designation. I need to be able to:

  1. Look in the file (easy enough)
  2. Count to the first tilde
  3. Look three characters back for the actual designator I need.

I have been looking at the commands, etc, but I am not finding anything help, as of yet.

Any thoughts would be greatly appreciated.

Upvotes: 0

Views: 33

Answers (2)

Walter A
Walter A

Reputation: 20022

First cut off all characters from the first tilde and the two characters before it. Next (ksh solution) use typeset to get the last character.

a="some string with 12345a~ and another ~ in it"
b="${a%%??~*}"
echo "Substring b: $b."
typeset -R1 desig 
desig=$b
echo "Found: ${desig}"

When typeset doesn't work (bash), you can also use

echo -n "$b"| tail -c1

Upvotes: 1

darkwolf29a
darkwolf29a

Reputation: 1

Ahh, nevermind.

I think I've figured out a way to go to a given character in the file, using cut -c.

That will get me dangerous. And then, from there, if necessary....I'll think about how the others.

Sorry to bother, but I will leave the answer here in case it will help others in the future.

Upvotes: 0

Related Questions