This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

awk not printing first character

 
Grace Green
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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 ...
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • 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
    • 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.

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic