LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-30-2015, 12:53 PM   #1
Shakai
LQ Newbie
 
Registered: Apr 2015
Posts: 5

Rep: Reputation: Disabled
Question 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
 
Old 10-30-2015, 01:28 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,515

Rep: Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545
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.
 
Old 10-30-2015, 02:18 PM   #3
Shakai
LQ Newbie
 
Registered: Apr 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by rknichols View Post
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 !
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Awk won't append bspears1 Programming 5 08-01-2012 01:33 PM
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 3 03-21-2012 01:47 PM
[SOLVED] Append text with AWK or SED? LostChild1 Linux - Newbie 6 03-20-2010 08:46 AM
Append to string in awk Zmyrgel Programming 2 09-20-2009 07:50 AM
How to append a string to a file name? anandkj Linux - Newbie 2 01-10-2008 08:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration