×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AWK suffix overwriting original text.

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:

CODE --> FILENAME}'

 
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.

RE: AWK suffix overwriting original text.

Hi

I guess the problem is that your file has Windows style new lines :

CODE --> command line

master # 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  cr  nl
         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 :

Quote (man awk)

RS          The input record separator, by default a newline. 

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

master # 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  cr  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  nl
         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 --> command line

master # awk -vRS='\r\n' '{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 --> command line

awk -vRS='\r\n' -vORS='\r\n' '{print $0 " -- " FILENAME}' test.txt 

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close