LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2022, 04:36 PM   #1
jgrace
LQ Newbie
 
Registered: Mar 2022
Posts: 14

Rep: Reputation: 0
Red face awk overwriting suffixes.


AWK suffixes are overwriting the original text.

I'm using awk to add the filename after the text on each line like this:
Code:
 awk '{print $0 " -- " 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 carriage return or newline carriage return pair and see the original text just fine:
Code:
 awk '{print $0 "\n -- " FILENAME}'
Here is a long line of text
-- test.txt
so I know the original data still exists. I can also prepend the text and that works fine. The problem only asserts itself as 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.

Last edited by jgrace; 03-15-2022 at 02:53 PM. Reason: Yes the close square bracket was supposed to end the "[code" tag so I was a typo sort of.
 
Old 03-14-2022, 05:16 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Welcome to LQ and the Programming forum!

Perhaps we should begin by asking the meaning of your example

Code:
awk '{print $0 " -- " FILENAME}']
Is the closing square bracket a typo? If not, how do you think it will be interpreted by your awk command?

Assuming it is a typo and your file is indeed named test.txt, I get this:

Code:
$ cat test.txt
Here is a long line of text

$ awk '{print $0 " -- " FILENAME}' test.txt
Here is a long line of text -- test.txt
It would be helpful if you would tell us the platform and version of awk you are using.

Also, check the test.txt file for presence of any non-printing special characters which might affect the positioning of the FILENAME output.
 
Old 03-14-2022, 07:10 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,146

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
The square bracket is part of a "test" in bash ?. So we're not being told the entire story ?. Maybe Windows text input ?. Maybe already (partially) munged ?.

Showing us a symptom without a proper description of the actual problem isn't going to help anyone.
 
1 members found this post helpful.
Old 03-15-2022, 03:14 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,989

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
that awk works ( without closing ] ), so if you have a problem with your script it is not that awk, but something else, which was not posted.
 
1 members found this post helpful.
Old 03-15-2022, 02:59 PM   #5
jgrace
LQ Newbie
 
Registered: Mar 2022
Posts: 14

Original Poster
Rep: Reputation: 0
The issue was that the file was created on a windows system and had carriage return line feeds at the end of each line. So the suffix printed after the line feed.
Adding {RS="\r\n"} to my awk statement fixed everything.

I appreciate the attention and support.

P.S. And I was trying to tell the entire story sysg00.
 
Old 03-16-2022, 01:02 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
A sub() deletes a \r at the end, so it will take a DOS or Unix input:
Code:
awk '{sub(/\r$/, ""); print $0 " -- " FILENAME}'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
[SOLVED] Overwriting free space or overwriting single files restored by photorec fcrok Linux - Security 22 09-15-2012 12:53 PM
Icann increases web domain suffixes Jeebizz Linux - News 0 06-20-2011 04:23 PM
looking for an apache directive or module related to appending file suffixes mattie_linux Linux - Server 1 09-20-2007 10:51 AM
what are %s, %u, %n suffixes? BroX Linux - Newbie 2 12-10-2003 06:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration