AWK suffix overwriting original text.
AWK suffix overwriting original text.
(OP)
I'm using awk to add the filename after the text on each line like this:
but the filename overwrites the original text starting at position zero.
The filename is "test.txt"
Example of text in the file:
Here is a long line of text
Expected output:
Here is a long line of text -- test.txt
But what I'm getting:
-- text.txtong line of text
I can add a a carriage return or newline carriage return pair and see the original text just fine:
Here is a long line of text -- test.txt
-- test.txt
so I know the original data still exists. I can also prepend the text and that works fine. The problem only asserts istself when suffixes are being added but I can't alter the positioning because I'm copying data into a database.
Any help would be appreciated.
Thanks in advance.
CODE --> FILENAME}'
The filename is "test.txt"
Example of text in the file:
Here is a long line of text
Expected output:
Here is a long line of text -- test.txt
But what I'm getting:
-- text.txtong line of text
I can add a a carriage return or newline carriage return pair and see the original text just fine:
Here is a long line of text -- test.txt
-- test.txt
so I know the original data still exists. I can also prepend the text and that works fine. The problem only asserts istself when suffixes are being added but I can't alter the positioning because I'm copying data into a database.
Any help would be appreciated.
Thanks in advance.
RE: AWK suffix overwriting original text.
I guess the problem is that your file has Windows style new lines :
CODE --> command line
While AWK's default record separator is a Unix style new line :
So what appears like this when displayed in terminal :
CODE --> command line
master # awk '{print $0 " -- " FILENAME}' test.txt -- test.txtng line of text
Is the carriage return character that is left in the middle of the text :
CODE --> command line
So all you have to do is let AWK know that you have 2 characters record separator :
CODE --> command line
Note that the above code will result text with Unix style new line. If you want to keep the Windows style new line in the output, you have to set the output record separator the same way :
CODE --> command line
Feherke.
feherke.github.io