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 |
What language are you trying to accomplish this in? You mention C but also other commands.
Regards, Alunduil |
Code:
[mherring@Ath ~]$ a="FEDERAL PACIFIC ELECTRIC PRODUCTS" |
Or you could just do it all in the awk:
Code:
echo $a | awk '{ print substr($1, length($1) - 4)}' |
[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 |
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 |
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 Here's the output of your sample text: Code:
$ ./Last5.sh FEDERAL PACIFIC ELECTRIC PRODUCTS |
Quote:
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 |
True, thanks for catching my inadvertent lies.
Regards, Alunduil |
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 |
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? |
Indeed ....
and here's another alternative - one tool, again. Code:
$ cat daniel |
: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.
|
Quote:
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 |
Quote:
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. |