wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes awk not printing first character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "awk not printing first character" Watch "awk not printing first character" New topic
Author

awk not printing first character

Grace Russo
Ranch Hand

Joined: Nov 08, 2010
Posts: 64
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

Joined: Aug 22, 2006
Posts: 257

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.


  • "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
    Grace Russo
    Ranch Hand

    Joined: Nov 08, 2010
    Posts: 64
    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.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: awk not printing first character
     
    Similar Threads
    Convert Into Java Code.
    Trees in Java
    What pid is using what port
    C2143 error and I can't find it
    Scala exercises for beginners (in FP)