2

I was wondering if anyone would be able to point out what is going wrong here. I have a txt file of data, delimited by a space, of 7 columns by 10000 rows. I want to add two extra columns one with the value 255.788 and 176.009 for every row entry.

I have used the following awk script:

#! bin/gawk -f

BEGIN {}
{ 
    print $0, 255.788, 176.009;
}
END{}

However this script adds the new values to the start of the following row. The new line character ^M appears at the end of the row of the old data like this;

1 274.458 165.208 256 192 305.989 142.202^M 255.788 176.009

2 276 164.278 256 192 305.963 142.037^M 255.788 176.009

Any help or ideas would be most appreciated!

In case this is confusing I want to achieve this:

1 274.458 165.208 256 192 305.989 142.202 255.788 176.009^M
  • ^M does not represent a \newline. That is the ASCII \return. newline and return are ASCII 12 and 15 in octal respectively. – mikeserv Oct 19 '14 at 8:01
  • Well when I import the data into excel/pages, both of them read this character as a new line and split my data over two rows. Any ideas? – Tim Oct 19 '14 at 8:05
  • MS systems typically do \r\n (I think it's in that order) at the end of a line. This is not the case on UNIX systems. The \returns, in fact, are likely to make processing difficult - especially if you're taking it a little bit at a time - because \return literally returns the cursor to the beginning of the line and so anything written to the terminal on that line before hand is then written over again. You'll want to remove those in most cases. There are tons of conversion utilities. Probably you already have dos2unix installed. – mikeserv Oct 19 '14 at 8:08
  • 1
    @mikeserv, for the record, it is \r\n, commonly referred to as CRLF. – HalosGhost Oct 19 '14 at 8:19
3

Just remove the CR before processing, and append it afterwards:

{
    gsub("\r","");
    print $0,255.788,176.009,"\r"
}
2

If you know that the data is CRLF terminated, then you could set awk's record separator accordingly in the BEGIN block

BEGIN {RS="\r\n";ORS=RS}
{ 
    print $0, 255.788, 176.009;
}
END{}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.