LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 04-04-2010, 09:39 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Rep: Reputation: 288Reputation: 288Reputation: 288
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
 
Old 04-04-2010, 10:11 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

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

Regards,

Alunduil
 
Old 04-05-2010, 12:05 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
[mherring@Ath ~]$ a="FEDERAL PACIFIC ELECTRIC PRODUCTS"
[mherring@Ath ~]$ echo $a|awk '{print $1}'|sed -r 's/.*(.{5}$)/\1/'
DERAL
 
Old 04-05-2010, 04:03 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Or you could just do it all in the awk:

Code:
echo $a | awk '{ print substr($1, length($1) - 4)}'
 
Old 04-05-2010, 06:27 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
[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
 
Old 04-05-2010, 10:41 AM   #6
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
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
 
Old 04-05-2010, 11:58 AM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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.
 
Old 04-05-2010, 09:42 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by alunduil View Post
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
 
Old 04-05-2010, 09:57 PM   #9
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
True, thanks for catching my inadvertent lies.

Regards,

Alunduil
 
Old 04-08-2010, 08:53 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
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
 
Old 04-08-2010, 09:31 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
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?
 
Old 04-08-2010, 12:52 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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
 
Old 04-08-2010, 01:48 PM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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.
 
Old 04-08-2010, 08:11 PM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by grail View Post
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
 
Old 04-08-2010, 08:26 PM   #15
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by PTrenholme View Post
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
 
  


Reply

Tags
efficiency, execution, time


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
awk printing from Nth field to last field sebelk Programming 2 01-08-2010 09:39 AM
LXer: Mastering Characters Sets in Linux (Weird Characters, part 2) LXer Syndicated Linux News 0 11-25-2009 11:30 PM
php question, how do I get a return from a field within a field? cherrington Programming 11 04-29-2009 01:27 AM
How to overwrite/change characters in the same field with C. slzckboy Programming 6 05-23-2005 05:07 PM


All times are GMT -5. The time now is 03:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration