1

I'm trying to parse some data from a dat file using awk, however I cant get rid of a linebreak that is being added to $0. I've tried gsub(/\n/,""), but it's done nothing.

Example below:

from dat file:

<A>1
<B>2

running:

awk '
BEGIN {FS = ">"; ORS=""; OFS=""}
/<A>/ {printf $2; printf$2}
' file.dat

currently gives me:

1
1

when I want:

11
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
WillSemple
  • 13
  • 2

1 Answers1

2

I think you just want

awk -F '>' '/<A>/ { print $2 $2 }' file.dat

This being said, your code should work, as well; the problem was that your input file contained DOS style newlines, which can be removed with, for example, dos2unix. See How to convert DOS/Windows newline (CRLF) to Unix newline (\n) in a Bash script? for more ways to do it.

Community
  • 1
  • 1
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • I've changed the FS and structured the print statement as in your example, but it doesn't do anything about the line break. I still get the same result. – WillSemple May 07 '17 at 01:53
  • Actually, your code works for me. Do you have DOS line endings with carriage returns? Does `cat -A file.dat` contain `^M` before the end of each line? – Benjamin W. May 07 '17 at 01:59
  • Yes must be the case. Just now I tested with gsub(/\r/,"") instead and it works. Wasn't aware of carriage returns before. Huge thanks for your help. – WillSemple May 07 '17 at 02:02