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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading and formatting lines from a file 2

Status
Not open for further replies.

lmb5kids

Programmer
Dec 18, 2003
36
US
I am trying to read a line at a time from a file and format the output only if it matches a certain pattern. Below is an example of a line :

R |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/77.77G |00000001/99.99G |00000000/ |00000000/99.99G |00000001/99.99E |00000006/99.99F |00000000/99.99J |00000000/88.88G |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000/ |00000000

I only need to print the numbers that are with the letters.
And the numbers and letters might not always be the same. The above example is only 1 line and the fields are seperated by a | .

Thanks for your help.
 
man awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for the reply, yes I have tried awk and I am having some porblems with it. I can get it to print the numbers with the letter but it prints this :
00000000/99.99G and all I need is the 99.99G. I am using this command : awk -F "|" -f search.awk file.txt and my search.awk file looks like this :
{print $1}
$2 ~ /99\./ || $2 ~ /77./ || $2 ~ /88./ {print $2}
and etc. for each field. Is there a better way ?

Thanks for your help.
 
A starting point:
{ print $1
for(i=2;i<=NF;++i)
if($i~"/[.0-9]+[A-Z] *$")
print substr($i,1+index($i,"/"))
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
nawk -f search.awk file.txt

search.awk:
Code:
BEGIN {
  FS="(\\|)|(/)"
}
{
  for(i=1; i <= NF; i++)
    if (match($i, /.*[^0-9].*/) && !match($i, /^ *$/))
       print $i
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
thanks to both of you those both work great. How would I go about putting all the matches on 1 line with a comma seperating each match :
99.99G,88.88G,99.99E, etc.

Thanks again.
 
{ printf "%s",$1
for(i=2;i<=NF;++i)
if($i~"/[.0-9]+[A-Z] *$")
printf ",%s",substr($i,1+index($i,"/"))
printf "\n"
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
BEGIN {
  FS="(\\|)|(/)"
  OFS=","
}
{
  for(i=1; i <= NF; i++)
    if (match($i, /.*[^0-9].*/) && !match($i, /^ *$/))
       printf("%s%s", $i, OFS)
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top