Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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?
[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.
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.
(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.
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 ...
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 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
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.