LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Keep last 'n' characters of a specified field (https://www.linuxquestions.org/questions/linux-newbie-8/keep-last-n-characters-of-a-specified-field-800034/)

danielbmartin 04-04-2010 09:39 PM

Keep last 'n' characters of a specified field
 
Hello.

The input file consists of several fields.
The first field is always five or more characters long.
I want to keep the last five characters of the first field.

Example:
If the input file record is FEDERAL PACIFIC ELECTRIC PRODUCTS
then I want to keep DERAL

I can do this in four steps by using C, REV, C, and REV again but that seems like doing an easy thing the hard way. Is there a straightforward way to achieve this result with C or some other command?

Daniel B. Martin

alunduil 04-04-2010 10:11 PM

What language are you trying to accomplish this in? You mention C but also other commands.

Regards,

Alunduil

pixellany 04-05-2010 12:05 AM

Code:

[mherring@Ath ~]$ a="FEDERAL PACIFIC ELECTRIC PRODUCTS"
[mherring@Ath ~]$ echo $a|awk '{print $1}'|sed -r 's/.*(.{5}$)/\1/'
DERAL


grail 04-05-2010 04:03 AM

Or you could just do it all in the awk:

Code:

echo $a | awk '{ print substr($1, length($1) - 4)}'

danielbmartin 04-05-2010 06:27 AM

[QUOTE=alunduil;3924353]What language are you trying to accomplish this in? You mention C but also other commands.

Thank you, Alunduil, for your interest. I am such a newbie that I don't even know how to respond. I am learning the powerful commands available in the LINUX environment. This is not a language per se, is it?

The reference to C should have been a reference to CUT, and not the C language. Mostly by a process of discovery I am learning commands such as CUT, PASTE, REV, TAC, and SED.

SED, by itself, has a breathtaking range of capability (and complexity).

Some day I will delve into AUK and GREP but for now they remain shrouded in mystery.

Daniel B. Martin

alunduil 04-05-2010 10:41 AM

The commands in the Linux environment are part of the shell you use which since I see you're using Ubuntu defaults to BASH. If you want to see some very cool things about BASH itself and how to accomplish various things I recommend you check out the Advanced BASH Scripting Guide.

Regards,

Alunduil

PTrenholme 04-05-2010 11:58 AM

If you're using bash, you can create a script to do what you want. For example, this script (Last5.sh) does (a littel more) then you asked:
Code:

#!/bin/bash
while [ $# -gt 0 ];do
  str=$1
  echo -n "${str} = "
  while [ ${#str} -gt 5 ];do
    str=${str#?}
  done
  echo ${str}
  shift
done

(The "little more" is that it gives you the last five characters of each input word, and will just print the word if it's less than five characters long.)

Here's the output of your sample text:
Code:

$ ./Last5.sh FEDERAL PACIFIC ELECTRIC PRODUCTS
FEDERAL = DERAL
PACIFIC = CIFIC
ELECTRIC = CTRIC
PRODUCTS = DUCTS

Note: To make the script file executable (so the ./ will run it), you need to do a chmod u+x Last5.sh befor it will run.

Tinkster 04-05-2010 09:42 PM

Quote:

Originally Posted by alunduil (Post 3924916)
The commands in the Linux environment are part of the shell you use which since I see you're using Ubuntu defaults to BASH.


That's actually a fairly inaccurate description; while most shells
do have a number of commands as 'built-ins' the tools like cut
& rev mentioned above are actually stand-alone executables ...



Cheers,
Tink

alunduil 04-05-2010 09:57 PM

True, thanks for catching my inadvertent lies.

Regards,

Alunduil

danielbmartin 04-08-2010 08:53 AM

Keep last 'n' characters of a specified field
 
Thanks for all the responses. My circumstances and application must be out of the ordinary. I'm new to Linux and attempting to port my Reginald/Windows REXX programs to Regina/Linux. This requires minor recoding. However, I'm coming to realise that large clumps of REXX code may be replaced with small clumps of Linux commands. The result is fewer lines of code and, in some cases, *much* shorter execution time.

I achieved the desired result with simple commands. This is a code snippet. If the style of coding looks strange, remember this was written to fit into a working REXX program.

"cut -d' ' -f1 <" AddrFile , /* Extract the House Numbers. */
"| rev" , /* Reverse numbers */
"| sed 's/$/0000/' ", /* Append four zeros */
"| cut -c1-5", /* Keep first 5 characters */
"| rev" , /* Reverse them again! */
">" hnFile /* Save in a work file */

The "last five" was done with rev, sed, cut, and anther rev. If you are certain that the field of interest is always at least five characters long the sed may be omitted. It's used as "insurance."

I was hoping that experienced Linux programmers knew of a clever coding for cut which would "take the last five" directly, without resorting to using rev. Apparently this is not do-able.

Daniel B. Martin

grail 04-08-2010 09:31 AM

Daniel I do not believe you have bothered to look at the replies people have posted.
I have counted 3 alternatives that appear more efficient than what you are doing.

Unless your objective has changed I am not sure why you would be using the convoluted code you have presented.

Have we perhaps misunderstood what you required?
Did you try any of the suggestions?

Tinkster 04-08-2010 12:52 PM

Indeed ....
and here's another alternative - one tool, again.
Code:

$ cat daniel
FEDERAL and then some
PACIFIC since we don't know
ELECTRIC the exact structure
PRODUCTS of what we're supposed to look at
$ sed -r 's/^[^ ]*([^ ]{5}).*/\1/' daniel
DERAL
CIFIC
CTRIC
DUCTS


PTrenholme 04-08-2010 01:48 PM

:scratch: Do you have some reason that you don't want to use one of the FOSS REXX interpreters available for Linux systems? (A quick "Google" for "rexx linux" will find you several.) I haven't looked in the Ubuntu repositories, but the Fedora repository contains the object-orientated extened REXX interpreter, oorexx. Try a simple sudo apt-get oorexx in your system, and you might get that one automatically installed.

danielbmartin 04-08-2010 08:11 PM

Quote:

Originally Posted by grail (Post 3928603)
Daniel I do not believe you have bothered to look at the replies people have posted.
I have counted 3 alternatives that appear more efficient than what you are doing.

Unless your objective has changed I am not sure why you would be using the convoluted code you have presented.

Have we perhaps misunderstood what you required?
Did you try any of the suggestions?

That's a rather uncharitable accusation.
I *did* look at other replies.
I did not use them because I don't understand them.
This is the Newbie Forum, and my understanding of Linux is
(at this point) quite limited. I'm a newbie.
I'm reluctant to put somebody else's code in my program
if I don't understand it. If I don't understand it,
I won't be able to change or extend it.

If/when I post more questions in this forum,
I will be more specific. I was looking for a clever coding
of the CUT command. I wasn't looking for awk or grep or Bash
or Perl or any other part of Linux which I haven't learned yet.
Maybe some day, but not now.

Daniel B. Martin

danielbmartin 04-08-2010 08:26 PM

Quote:

Originally Posted by PTrenholme (Post 3928865)
:scratch: Do you have some reason that you don't want to use one of the FOSS REXX interpreters available for Linux systems? (A quick "Google" for "rexx linux" will find you several.) I haven't looked in the Ubuntu repositories, but the Fedora repository contains the object-orientated extened REXX interpreter, oorexx. Try a simple sudo apt-get oorexx in your system, and you might get that one automatically installed.

I never heard of FOSS REXX interpreters, and that's why I didn't use one, or try one. Is there good reason to think they are superior to Regina?

I know zero about Object Oriented programming, so an Object Oriented REXX interpreter would not be helpful.

Unlike some (many?) (most?) participants in this forum, I am almost alone in my effort to learn Linux. I dabble in programming as a hobby. Being well into retirement (15 years, now) I have no work colleagues to ask. My sole source of personal guidance is Email from a friend who lives 600 miles away. He sternly warns me against using sudo for any purpose.

Daniel B. Martin


All times are GMT -5. The time now is 12:55 AM.