Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

separate humber from alphabets 2

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
Hello Everybody

I have a data format which looks like this

$NAME COMPS 4door
$NAME COMPS 5feet_rear
$NAME COMPS 6feet_front
etc...

I need to get only the door,feet_rear,feet_front values from the 3rd field above and not the numbers 4,5,6.
How do I separate the number from the alphabets.

also
If I have a file called m1
a b

and a file called m2
c d

how do I get a file called
m3 which is
a b c d

cat command to perform above operation shows
a b
c d

Regards
Murugs
 
1) Try this awk script
Code:
{
  sub(/[^0-9]+/,"",$3)
  print $3
}
2) Maybe something like
Code:
{ printf $0 " " }
You might want to add a END{print ""} if you need a linefeed at the end of the line. CaKiwi
 
Murugs:

If you want to drop the first character from field 3, you can use the substring function and ask from character 2 to the end of the field:

awk '
{
printf("%s\n", substr($3, 2, length($3)))
} ' nfile

It get's a little more difficut if the number is over 1 digit. Of course, the brute force method would be to check the characters at the begging of $3 until you hit something non-numeric.

Regards,

Ed
 
CaKiwi:

Yours is better than mine, that's why you get the star!

Ed
 
Hi all,

1) I think CaKiwi`s solution is fine (it saves one function call which slows performance).
I would change it to:
Code:
  AWK '{sub(/[0-9]+/,"",$3);
        print $3;
       }'

We removed the caret "^" which NEGATES the regex range. I think Murugs wants TEXT not numbers, right?

2) It is possible for AWK to work with two opened files:
Code:
   m1 - is processed by command line argument. I assume
        m1 is LONGER than m2.
   m2 - is opened inside AWK script
This can be accomplished with `getline' command. If you decide to use it ... WELCOME to NON-begginer's World!!! According to AWK book Effective AWK Programming:

``Introduction to getline
This command is used in several different ways, and
should not be used by beginners...''

We will use a variant of getline command which reads the
content of ONE line of file ``m2'' into variable ``m2line'':
Code:
    getline m2line < &quot;m2&quot;
If getline is sucessful then variable will be assigned the whole content of line and the pointer will move to the next line of m2. Otherwise, if there is no more lines to read, getline returns 0 (zero). Usually this is used inside a loop with WHILE:
Code:
   while((getline m2line < &quot;m2&quot;) > 0){
      print m2line;
   }
Some care should be taken if the files do not have the same
number of lines. Use `wc -l m1 m2' to be sure.
Code:
   m1      m2
   ---     ----
   a b     1 2
   c d     3 4 5
   e f
   
AWK '{ 
  printf $0 OFS
  if((getline m2line < &quot;m2&quot;) > 0 ) printf m2line 
  printf &quot;\n&quot;  
  }
END{ 
  close(&quot;m2&quot;)
  }' m1
Please check the following:

m2line isn't parsed for fields (AFAIK), so if you want
the output with comma delimited you should pipe the
output to another awk simple script or split m2line
into an Array (another NON-Beg stuff).


Sorry for the blah, blah, blah...

Jayr Magave
 
Hi all,

A bit off-topic, but...

I am brazilian, and I wish the best for my
american friends on this 4th July.


May God bless your nation (provided Bush don't
mess up things too much :)

Strength and Honor!

Jayr Magave
 
On the second question, look into using *NIX 'paste' - man paste
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I should know *NIX had something, 'cause this sound
pretty mundane task. I thought
Code:
join
could work, but it requires common fields. Alas,
Code:
paste[code][/b] does the job (it even works on my [i]cygwin[/i]).

I [b]do[/b] read man pages... but someone must tell you
they exist for the needed task. A star to vgersh99!

Jayr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top