Kirk Britton
Kirk Britton

Reputation: 1

Remove the newline character in awk

I am trying to remove the new line character for a date function and have it include spaces. I am saving the variables using this:

current_date=$(date "+%m/%d/ AT %y%H:%M:%S" )

I can see that this is the right format I need by doing a echo $current_date. However, when I need to use this variable it does not act the way I would like it.

awk '(++n==47) {print "1\nstring \nblah '$current_date' blah 2;     n=0} (/blah/) {n=0} {print}' input file > output file 

I need the date to stay in the current line of text and continue with no newline unless specified.

Thanks in advance.

Upvotes: 0

Views: 1942

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74615

Rather than attempting to insert the variable into the command string as you are doing, you can pass it to awk like this:

awk -v date="$(date "+%m/%d/ AT %y%H:%M:%S")" '# your awk one-liner here' input_file

You can then use the variable date as an awk variable within the script:

print "1\nstring \nblah " date " blah 2";

As an aside, it looks like your original print statement was broken, as there were double quotes missing from the end of it.

Upvotes: 3

Related Questions