Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK suffix overwriting original text.

Status
Not open for further replies.

johnegracejr

Systems Engineer
Mar 14, 2022
1
0
0
US
I'm using awk to add the filename after the text on each line like this:
Code:
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.
 
Hi

I guess the problem is that your file has Windows style new lines :
Code:
[blue]master #[/blue] od -tax1 test.txt 
0000000   H   e   r   e  sp   i   s  sp   a  sp   l   o   n   g  sp   l
         48  65  72  65  20  69  73  20  61  20  6c  6f  6e  67  20  6c
0000020   i   n   e  sp   o   f  sp   t   e   x   t  [highlight lime]cr[/highlight]  [highlight cyan]nl[/highlight]
         69  6e  65  20  6f  66  20  74  65  78  74  0d  0a
0000035

While AWK's default record separator is a Unix style new line :
man awk said:
[pre]RS The input record separator, by default a newline.[/pre]

So what appears like this when displayed in terminal :
Code:
[blue]master #[/blue] 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:
[blue]master #[/blue] awk '{print $0 " -- " FILENAME}' test.txt | od -tax1
0000000   H   e   r   e  sp   i   s  sp   a  sp   l   o   n   g  sp   l
         48  65  72  65  20  69  73  20  61  20  6c  6f  6e  67  20  6c
0000020   i   n   e  sp   o   f  sp   t   e   x   t  [highlight lime]cr[/highlight]  sp   -   -  sp
         69  6e  65  20  6f  66  20  74  65  78  74  0d  20  2d  2d  20
0000040   t   e   s   t   .   t   x   t  [highlight cyan]nl[/highlight]
         74  65  73  74  2e  74  78  74  0a
0000051

So all you have to do is let AWK know that you have 2 characters record separator :
Code:
[blue]master #[/blue] awk [highlight]-vRS='\r\n'[/highlight] '{print $0 " -- " FILENAME}' test.txt 
Here is a long line of text -- test.txt

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:
awk [highlight]-vRS='\r\n'[/highlight] [highlight]-vORS='\r\n'[/highlight] '{print $0 " -- " FILENAME}' test.txt


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top