Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk printing redirection in awk script

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
GB
Hello All,

I thought I had this working, but it doesn't want to redirect the output to a series of files.

Background is: I have a text file from which I want to extract 2 variables, $4 and $6. For each instance of $4 I want to print the contents of the variables $4 and $6 to an external file named using the $4 variable.

I have the bulk of the script working, but I cannot get the script to generate and print the contents of the $4 and $6 variables to the individual files..

I have attached a copy of the code below.

BEGIN {
}
{
contact_name=$4
contact_number=$6
file_name=$4".vcf"
{ print "N:"$4"\n" "TEL;CELL;VOICE:"$6 > $file_name }
}
END {
}

Any help would be appreciated.

Regards

Alf

PS: I am running awk with the -f parameter and submitting the source data file on the command line when the script is run.
 
Replace this:
> $file_name
By this:
> file_name

Seems you don't need the contact_XXX variables, so the whole awk program could be as simple as this:
{ print "N:"$4"\nTEL;CELL;VOICE:"$6 > $4".vcf" }


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hello there,

many thanks for your reply. I have quickly tried this as follows and the following error is generated.

awk '{ print "N:"$4"\nTEL;CELL;VOICE:"$6 > $4".vcf" }' PhoneBook.ncc

awk: cmd. line:1: (FILENAME=PhoneBook.ncc FNR=1) fatal: expression for `>' redirection has null string value

It doesn't like the redirection. I am running this script of Redhat Linux, but surely this shouldn't be a problem. in terms of compatibility ??

Thanks and regards

Alf
 
And this ?
awk 'NF>5{print "N:"$4"\nTEL;CELL;VOICE:"$6 > $4".vcf"}' PhoneBook.ncc

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hello again,

still got a problem with, as follows;

[netadmin@ns1 netadmin]$ awk 'NF>5{print "N:"$4"\nTEL;CELL;VOICE:"$6 > $4".vcf"}' PhoneBook.ncc
awk: cmd. line:1: (FILENAME=PhoneBook.ncc FNR=1) fatal: expression for `>' redirection has null string value

weird !!

Regards

Alf
 
OK, works on gawk, not on nawk.
You may try this instead:
awk 'NF>5{out=$4".vcf";print "N:"$4"\nTEL;CELL;VOICE:"$6 > out}' PhoneBook.ncc

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

This is a usual error when reading, but is possible to be the same. Try to enclose the expression between parenthesis :
Code:
{ print "N:"$4"\nTEL;CELL;VOICE:"$6 > [red]([/red]$4 ".vcf"[red])[/red] }

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top