• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

awk not printing first character

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have files that need to be processed by the awk script below, but the outcome is such that the awk script:
a. places a pipe | at the beginning of the line when I do not expect it
b. does not print the first character of the first field
Any help understanding why a & b happen would be very much appreciated.

My input lines are similar to this one:
7035ABC071222|AB4801|123|E|I|1|SER|1|BAKBAA1|20990220||MONSTER INC||PP02X||||||008354||0001||N|MPC|0|
7035ABC071222|AB4801|123|P|EP|6|SER|1|BA5JAA1|20991228||MONSTER INC||LATITUDE E3810|20110427|||||008523||BLDG 15||Y|MPC|0|

The code is:



The output looks like :

THIS IS THE FIRST 7035ABC071222
|035ABC071222|AB4801|123|E|I|1|SER|1|BAKBAA1|20990220||MONSTER INC||PP02X||||||008354||0001||N|MPC|0|

THIS IS THE FIRST 7035ABC071222
|035ABC071222|AB4801|123|P|EP|6|SER|1|BA5JAA1|20991228||MONSTER INC||LATITUDE E3810|20110427|||||008523||BLDG 15||Y|MPC|0|

WHy would a[1] print "7035ABC071222", but a[x] prints "|035ABC071222" ?

Thank you in advance, and yes, I am not very familiar with awk. If fact, if I do not figure this out I can write it in something else ...
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to explain why you get the output that you are getting. That said, you may want to consider posting in comp.lang.awk with the following details:
  • Which awk are you using? What OS? They all have their quirks.
  • Also, suggest you format your code when you post there (you have done so, but the indentation is off at places).


  • Some ideas that you can try:

  • You don't have to use split in your case. You can simply overwrite the contents of your field with the result of substr. That way, a simple "print;" at the very end would work regardless of your if-else case.
  • Your for loop should go from 1 to n, not 1 to NF. Ditto for your if conditions that follow the call to split. Note that you have empty fields. I do not know what split does when it encounters multiple separators contiguously.
  •  
    Grace Green
    Ranch Hand
    Posts: 79
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for your help.
    I have foudn the problem. the input file contained CRLF at the end of each line and the script I posted would not correctly split where it should have.

     
    reply
      Bookmark Topic Watch Topic
    • New Topic