0

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/%y AT %H:%M:%S" )

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

current_date=$(date "+%m/%d/%y AT %H:%M:%S" )    
awk '(++n==2) {print "1\nData \nAccount '$current_date' Terminated;     n=0} (/blah/) {n=0} {print}' input file > output file

Input:

Line 1
Line 2
Line 3

Output:

Line 1
Line 2
Data
Account '$current_date' 
Terminated 
Line 3

Desired Output:

Line 1
Line 2
Data
Account '$current_date' Terminated 
Line 3
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • It is not possible for the script you posted to produce the output you posted because it contains a syntax error. Fix the syntax error and then run the script and then post the script and its output. Right now we're trying to help you debug a script we haven't seen which is producing impossible output given what you have told us so far - it's extremely unlikely we will succeed. – Ed Morton Mar 24 '15 at 00:05
  • `awk 'NR==3{print "data\nAccount "strftime("%m/%d/%y AT %H:%M:%S")" terminated"}1'` –  Mar 24 '15 at 08:13

2 Answers2

2

Rather than try to use shell-syntax to put a shell variable into awk code, it is often much simpler and safer to simply assign the shell variable to an awk variable with the -v option:

$ awk -v d="$current_date" '{print} (++n==2) {printf "Data \nAccount %s Terminated\n",d; n=0} (/blah/) {n=0}' file 
Line 1
Line 2
Data 
Account 03/23/15 AT 14:34:10 Terminated
Line 3

Removing superfluous newlines from the variable current_date

Suppose that we add superfluous newlines to current_date:

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

We can remove them as follows:

$ awk -v d="$current_date" 'BEGIN{sub(/\n/,"",d)} {print} (++n==2) {printf "Data \nAccount %s Terminated\n",d; n=0} (/blah/) {n=0}' file 
Line 1
Line 2
Data 
Account 03/23/15 AT 15:41:17 Terminated
Line 3
John1024
  • 109,961
  • 14
  • 137
  • 171
  • When I run your code I do not get your output, but rather the output I have been receiving where it places Terminated on a new line – Skonectthedots Mar 23 '15 at 22:31
  • @Skonectthedots I cannot reproduce that but please see updated answer for a method that is more aggressive about removing unwanted newline characters. If that doesn't work, please update your question with the results of `declare -p current_date`. – John1024 Mar 23 '15 at 22:47
  • @John1024 Would need gsub if there was more than one newline as implied by `we add superfluous newlines` –  Mar 24 '15 at 11:18
0

I had to add 3 double-quotes in your awk command:

awk '(++n==2) {print "1\nData \nAccount '"$current_date"' Terminated";     n=0} (/blah/) {n=0} {print}' foo.txt

When you closed the single-quote and re-opened it right before and after $current_date you needed to put double-quotes around the variable so it would hold the tokens together around the spaces. Then you needed another quote after Terminated to finish the string.

I should add that I was getting syntax errors before I made these changes, so perhaps there's something else going on...

Peter Bowers
  • 3,063
  • 1
  • 10
  • 18
  • 1
    Its not a good idea using variable this way in `awk` code. See John1024s answer or my answer here: http://stackoverflow.com/questions/19075671/how-to-use-shell-variables-in-awk-script – Jotne Mar 24 '15 at 06:23