LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk append string (https://www.linuxquestions.org/questions/linux-newbie-8/awk-append-string-4175557581/)

Shakai 10-30-2015 12:53 PM

awk append string
 
Hi,

I'm trying to add a string after this AWK result:

$ awk '/H005/ {print $0;}' 010.txt
|H005|31032015|42498804,50|01|

I'm trying this way:

$ awk '/H005/ {print $0, "test";}' 010.txt

But AWK is printing the string before the result and overwriting the first and second column:

$ awk '/H005/ {print $0, "test";}' 010.txt
test|31032015|42498804,50|01|

how could i append the STRING to the end of the result ?
|H005|31032015|42498804,50|01|test

rknichols 10-30-2015 01:28 PM

I strongly suspect that the source file has DOS line endings and the last character of the line is a carriage return. That will make it appear that your string is replacing the start of the line, whereas in fact it is not. You need to run the dos2unix utility on the file to convert the line endings, or else filter out the carriage return in the awk script.
Code:

awk '/\r$/ {gsub("\\r$", "")} /H005/ {print $0 "test"}'
Note that you probably do not want a "," in that print statement as it would cause awk to insert a separator (default: space) in the output rather than doing simple string concatenation.

Shakai 10-30-2015 02:18 PM

Quote:

Originally Posted by rknichols (Post 5442485)
I strongly suspect that the source file has DOS line endings and the last character of the line is a carriage return. That will make it appear that your string is replacing the start of the line, whereas in fact it is not. You need to run the dos2unix utility on the file to convert the line endings, or else filter out the carriage return in the awk script.
Code:

awk '/\r$/ {gsub("\\r$", "")} /H005/ {print $0 "test"}'
Note that you probably do not want a "," in that print statement as it would cause awk to insert a separator (default: space) in the output rather than doing simple string concatenation.


You are right, the file was created on windows.

The script worked perfectly, thank you !


All times are GMT -5. The time now is 05:14 PM.