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

Text file 2

Status
Not open for further replies.

sabetik

IS-IT--Management
Nov 16, 2003
80
GU
I have the following text:

Field#1 #2 #3 #4
________________________________________________
1110718836003 T 001110718857003 000000513
1111010D010 T 0 000033106
Like to convert to :

"11071-88836-003","5.13"
"11101-0D010","331.06"

Ignore the first character of field#1. if filed1 lenght is 12 spaces display the filed1 as xxxx-xxxx-xx if field is 10 spaces display as xxxxx-xxxxx. On field#4 display as numeric value (ex:0000000513 make it: 5.13)

Thanks again
 

Rather interesting problem, sabetik. Have a star!


Code:
# Execute the code if field 4 exists.
$4 \
{ # Remove first character.
  sub( /./, "" )
  # Insert "-" after each group of 5 chars.
  gsub( /...../, "&-", $1 )
  # Remove any trailing "-".
  sub( /-$/, "", $1 )
  printf "\"%s%\",\"%.2f\"\n", $1, $4/100
}

Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. For an introduction to Awk, see faq271-5564.

 
Good job but I forgot to mentioned that after the field#4 I have other field that I do not care. the problem is field$4 is only 10 space and right after the field other field follow with no field seperator.
--------
like this 0000011410000101995011990068ZZ6
field#4 is 00000114
 
Code:
# Execute the code if field 4 exists.
$4 \
{ # Remove first character.
  sub( /./, "" )
  # Use only first 10 chars. of field 4.
  $4 = substr( $4, 1, 10 )
  # Insert "-" after each group of 5 chars.
  gsub( /...../, "&-", $1 )
  # Remove any trailing "-".
  sub( /-$/, "", $1 )
  printf "\"%s%\",\"%.2f\"\n", $1, $4/100
}
Does this work properly?
 
One more thing. In output file there is extra %
"45286-32919-04%","272.61"
"45286-33120-E0%","124.71"

 
[tt]
# Execute the code if field 4 exists.
$4 \
{ # Remove first character.
sub( /./, "" )
# Use only first 10 chars. of field 4.
$4 = substr( $4, 1, 10 )
# Insert "-" after each group of 5 chars.
gsub( /...../, "&-", $1 )
# Remove any trailing "-".
sub( /-$/, "", $1 )
printf "\"%s\",\"%.2f\"\n", $1, $4/100
}
[/tt]

 
Is it possible to compare the result with another file. the reason I am asking I have another file that has more informations of the file I am working. I get these two files from two diffrent system. my other file is like this

"45286-32919-04%","272.61","102","breaks for two wheels"

you see I like to match first field of the first file with the first field of the second file if match get the extra info. I know it will be difficult. but I have seen wonder from you guys.

Thanks
Kamran
 

The file with the extra information should be listed first on the command line. Example:
[tt]
awk -f prog.awk extra_info main_file
[/tt]

Code:
BEGIN {
  # How many files are we reading?
  # Should be 1 or 2; if 2, the first one
  # has extra information.
  numfiles = ARGC - 1
}

# Are we reading the 1st of 2 files?
# It should be the file with extra info.
2==numfiles && NR==FNR {
  p = index( $0, "\",\"" )  
  if ( p )
  { key = substr($0,2,p-2)
    # Remove any trailing "%".
    sub( /%$/, "", key )
    extra_info[ key ] = substr( $0, p+2 )
  }
  next
}


# Execute the code if field 4 exists.
$4 \
{ # Remove first character.
  sub( /./, "" )
  # Use only first 10 chars. of field 4.
  $4 = substr( $4, 1, 10 )
  # Insert "-" after each group of 5 chars.
  gsub( /...../, "&-", $1 )
  # Remove any trailing "-" we added.
  sub( /-$/, "", $1 )
  printf "\"%s%\",\"%.2f\"", $1, $4/100
  #
  # First file may have had info. about this item.
  #
  if ( $1 in extra_info )
    printf " ==> %s", extra_info[ $1 ]
  #
  print ""
}
 
Oops. One of my "printf"'s had an extra % again.
Code:
BEGIN {
  # How many files are we reading?
  # Should be 1 or 2; if 2, the first one
  # has extra information.
  numfiles = ARGC - 1
}

# Are we reading the 1st of 2 files?
# It should be the file with extra info.
2==numfiles && NR==FNR {
  p = index( $0, "\",\"" )  
  if ( p )
  { key = substr($0,2,p-2)
    # Remove any trailing "%".
    sub( /%$/, "", key )
    extra_info[ key ] = substr( $0, p+2 )
  }
  next
}


# Execute the code if field 4 exists.
$4 \
{ # Remove first character.
  sub( /./, "" )
  # Use only first 10 chars. of field 4.
  $4 = substr( $4, 1, 10 )
  # Insert "-" after each group of 5 chars.
  gsub( /...../, "&-", $1 )
  # Remove any trailing "-" we added.
  sub( /-$/, "", $1 )
  printf "\"%s\",\"%.2f\"", $1, $4/100
  #
  # First file may have had info. about this item.
  #
  if ( $1 in extra_info )
    printf " ==> %s", extra_info[ $1 ]
  #
  print ""
}
 
futurelet, what I am doing wrong. No print out:

here my extra_info file :
"71812-89110-S4","7181289110B0","100"
"71812-42020-B1","SHIELD, FR SEAT CUSH","107"

here my main_file:
"71812-89110-S4","112.71"
"71812-42020-B1","177.11"

Thanks
 
If on an *nix box: man join

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If you're playing with an Unix like OS then take a look at the join command.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
No I am using dos awk
:-(
Anyway you have already all the stuff you need in this thread: populating an associative array and then testing the existence of extra info.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Main file:

1110718836003 T 001110718857003 000000513xxxxx
1111010D010 T 0 000033106xxxxx

extra_info:

"71812-89110-S4","7181289110B0","100"
"71812-42020-B1","SHIELD, FR SEAT CUSH","107"
 
Thanks for all the help. I should be able to follow the program.

Thanks
Kamran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top