Awk Tab delimiter not working

Hi All,

I am having trouble with the AWK tab delimiter.

When I run this, my output looks like this:

However, if I remove one of the fields ("\t|, $timefield) then my output is correct with no new line being put in between the date and the next string, like so.

Any thoughts or help would be greatly appreciated. Thank you for your help!

It seems like you define your input field separator wrong. Here en example of how it will work:

set tabbedData to "id	field1	field2	field3	field4
20	john	doe	mainstreet	london
NULL	NULL	NULL	NULL	NULL
21	john	marksson	secondstreet	new york
22	mark	johnsson	thirdstreet		hong kong"

do shell script "awk -F " & quoted form of tab & " '
{
	if ($1 != \"NULL\" && NR != 1)
		print($2, $3, $4, $5)	  
}' <<<" & quoted form of tabbedData

To get the output tabbed separated, you only need to set the output field separator


set tabbedData to "id	field1	field2	field3	field4
20	john	doe	mainstreet	london
NULL	NULL	NULL	NULL	NULL
21	john	marksson	secondstreet	new york
22	mark	johnsson	thirdstreet	hong kong"

do shell script "awk -v 'OFS=" & tab & "' -F " & quoted form of tab & " '
{
	if ($1 != \"NULL\" && NR != 1)
		print($2, $3, $4, $5)	  
}' <<<" & quoted form of tabbedData

*because the question is not complete, this is only what I have.

Thank you so much for your reply. I am pretty new to awk and am struggling to understand how this all works, so I apologize for the lack of information. Presently, I am using an awk script named: “awktest.awk”. In terminal, I am running the command (in the same directory):

administrator$ awk -f awktest.awk 022314.txt > test.txt

It seems that the way you are defining and building awk within Applescript might be the better way to go. The text file (022314.txt) I am parsing basically is this (slightly longer):

I then run the awktest.awk which is this:

output

Generally, the “\t” seems to work, but when I add the last field with “\t”, it seems to add a new line instead. What I don’t understand is why there is a newline after the date ($datefield) and time($timefield) when I have the tab delimiter (“\t”) after the fields.