AWK specific output filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK specific output filename
# 1  
Old 08-10-2012
AWK specific output filename

Hi All,

I'd like to create a specific output filename for AWK.

The file I am processing with AWK looks like:

Code:
output_081012.csv*
27*TEXT*1.0*2.0*3.0

where * is my delimeter and the first line of the file is the output filename i'd like to create

is there a way to assign an awk variable to the first line and then use that variable in the printf command to create the output file?

for instance
Code:
awk -f inputfile
BEGIN
{
FS='*'
if (FNR==1)
outputfile=$1
}
{
if {FNR==2}
printf("%s,%s,%s,%s,%s\n",$1,$2,$3,$4,$5) >>outputfile
}

Thanks!
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 08-10-2012 at 02:23 PM..
# 2  
Old 08-10-2012
Well, you don't put that in the BEGIN -- that runs before any files are processed, not during. You can use OFS to simplify your printf into a print, too.

Code:
awk -F"*" -v OFS="," 'NR==1{F=$1; next} { print $1,$2,$3,$4,$5>F }' input

# 3  
Old 08-10-2012
I think Corona688's keyboard is dropping characters today... I think he meant:
Code:
awk -v FS="*" -v OFS="," 'NR==1{F=$1; next} { print $1,$2,$3,$4,$5>F }' input

I noticed that you attempt at the code used FNR==1 instead of NR==1. If you intended to process multiple input files in a single call to awk and to have awk append to a different output file based on the first line of each input file, I think you want something like:
Code:
awk -v FS="*" -v OFS="," 'FNR==1 {
	if (F != "") close(F);
	F=$1
	next
}
	{ print $1,$2,$3,$4,$5>>F}' input_file1 input_file2 input file3 ...

Note also that you don't need the "*" at the end of the first line in your input file. (It doesn't hurt to have it, it just isn't needed for the script to work.)
# 4  
Old 08-10-2012
-F"*" is perfectly valid. It's short-form for -v OFS="*", not to mention probably older.

Not that I mind you catching my other typos. Smilie

Another funny awk thing you might see sometimes is awk '{print $1}' VARNAME="asdf" filename which looks weird but is also a perfectly good way of setting a variable inside awk, and probably older than -v. Just remember that they're parsed the same time as filenames -- i.e. they won't be parsed before a BEGIN {} block. -v VAR=whatever, on the other hand, gets parsed before BEGIN {}.

Last edited by Corona688; 08-10-2012 at 03:25 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-10-2012
Quote:
Originally Posted by Corona688
-F"*" is perfectly valid. It's short-form for -v OFS="*", not to mention probably older.

Not that I mind you catching my other typos. Smilie

Another funny awk thing you might see sometimes is awk '{print $1}' VARNAME="asdf" filename which looks weird but is also a perfectly good way of setting a variable inside awk, and probably older than -v. Just remember that they're parsed the same time as filenames -- i.e. they won't be parsed before a BEGIN {} block. -v VAR=whatever, on the other hand, gets parsed before BEGIN {}.
I apologize, -F ERE is a synonym for -v FS=ERE (and it is documented in all of the man page including the POSIX/UNIX standards). I assume you had a typo above and meant FS rather than OFS. Smilie When I copied your solution into a file and tried it out, it failed; I must have screwed up something in the cut and paste.

Yes, I know that variables can also be set after the awk program on the command line. In fact you can intermix variable assignment operands and pathname operands. Variable assignments that appear here are processed after any commands specified the the awk program's BEGIN block and before any following file operands are read by the program. So you could have a command line like:
Code:
awk ' {$(NF+1)=F;print}' F=file1 file1 F=file2 file2

to cat files with the filename appended to each line in the file. This is documented in the POSIX standard but isn't mentioned on many vendor man pages.
# 6  
Old 08-10-2012
I left out some details but I basically have a bunch of *.csv's that I am trying to collect together into one file. The format of each *.csv matches what I posted earlier, where the filename is the first record and the second line is the data. Is there a good way to add a header row at the top of the output file? For some reason I don't believe my shell is working the way it is supposed to, so I am resorting to calling awk once to create the output file with the header row and then on the second call to populate it. Either way, thanks for your help!

---------- Post updated at 04:58 PM ---------- Previous update was at 04:47 PM ----------

My awk script looks like:
BEGIN{
RS="\n"
FS="*"
OFS=","
ST1="Channel Number"
ST2="Channel Label"
ST3="Time at Max"
ST4="Time History Max"
ST5="Time at Min"
ST6="Time History Min"
ST7="Frequency at Max Response"
ST8="Max Response"
}
{
if (FNR==1)
outputfile=$1
print ST1 ST2 ST3 ST4 ST5 ST6 ST7 ST8 >outputfile
if (FNR==2)
print $1 $2 $3 $4 $5 $6 $7 $8 >>outputfile
}

I thought this would work but it doesn't
# 7  
Old 08-10-2012
Quote:
Originally Posted by LMSteed
I left out some details but I basically have a bunch of *.csv's that I am trying to collect together into one file. The format of each *.csv matches what I posted earlier, where the filename is the first record and the second line is the data. Is there a good way to add a header row at the top of the output file? For some reason I don't believe my shell is working the way it is supposed to, so I am resorting to calling awk once to create the output file with the header row and then on the second call to populate it. Either way, thanks for your help!

---------- Post updated at 04:58 PM ---------- Previous update was at 04:47 PM ----------

My awk script looks like:
BEGIN{
RS="\n"
FS="*"
OFS=","
ST1="Channel Number"
ST2="Channel Label"
ST3="Time at Max"
ST4="Time History Max"
ST5="Time at Min"
ST6="Time History Min"
ST7="Frequency at Max Response"
ST8="Max Response"
}
{
if (FNR==1)
outputfile=$1
print ST1 ST2 ST3 ST4 ST5 ST6 ST7 ST8 >outputfile
if (FNR==2)
print $1 $2 $3 $4 $5 $6 $7 $8 >>outputfile
}

I thought this would work but it doesn't
You're close. You have a few problems:

First, the expressions passed to print need to be separated by a comma.

Second, you print the headerline to outputfile twice (because you're missing a { } pair around the commands you want to run when FNR is 1.

Third, you aren't closing any of the output files you're opening. With a small number of files, it won't matter since all open files will be closed when you get to the end. But if you have a large number of files, you may run out of file descriptors.

The default value for RS is a <newline>, so you don't need to set it.

I've made a couple of other slight changes and reformatted to make it easier to read, but this is VERY similar to what you did:
Code:
BEGIN{
    FS="*"
    OFS=","
    ST1="Channel Number"
    ST2="Channel Label"
    ST3="Time at Max"
    ST4="Time History Max"
    ST5="Time at Min"
    ST6="Time History Min"
    ST7="Frequency at Max Response"
    ST8="Max Response"
}

FNR==1 {
    if (output file!="") close(outputfile)
    outputfile=$1
    print ST1,ST2,ST3,ST4,ST5,ST6,ST7,ST8 >outputfile
}
FNR==2 {
    print $1,$2,$3,$4,$5,$6,$7,$8 >>outputfile
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create separate files but not include specific field in output

I am trying to use awk to create (in this example) 3 seperate text file from the unique id in $1 in file, if it starts with the pattern aa. The contents of each row is used to populate each text file except for $1 which is not needed. It seems I am close but not quite get there. Thank you :). ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to output match and mismatch with count using specific fields

In the below awk I am trying output to one file those lines that match between $2,$3,$4 of file1 and file2 with the count in (). I am also trying to output those lines that are missing between $2,$3,$4 of file1 and file2 with the count of in () each. Both input files are tab-delimited, but the... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. UNIX for Dummies Questions & Answers

awk : dynamic output flatfile filename

Hello, I'm using the awk command to insert empty columns on a tab delimited flatfile - which works fine - => But I'm not able to manage dynamicaly the filename of the awk output based on the source flatfile filename I have 3 source flatfile: flatfile_Jan-2016.csv flatfile_Feb-2016.csv... (3 Replies)
Discussion started by: Tipiak
3 Replies

4. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

5. Shell Programming and Scripting

awk to output specific matches in file

Using the attached file, the below awk command results in the output below: I can not seem to produce the desired results and need some expert help. Thank you :). awk -F'' ' { id += $4 value += $5 occur++ } END{ printf "%-8s%8s%8s%8s\n", "Gene", "Targets", "Average Depth", "Average... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. UNIX for Advanced & Expert Users

Problem piping find output to awk, 1st line filename is truncated, other lines are fine.

Today I needed to take a look through a load of large backup files, so I wrote the following line to find them, order them by size, and print the file sizes in GB along with the filename. What happened was odd, the output was all as expected except for the first output line which had the filename... (4 Replies)
Discussion started by: gencon
4 Replies

7. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

8. Shell Programming and Scripting

Getting a specific date from cal output with AWK

Hi guys! I'll make this short... Is there any good way to get the day number that first matches the Monday column from the cal command output with awk (or any other text manipulator commands) ? I'm sorry if my question wasn't clear at all. For example... One cal output would be $... (6 Replies)
Discussion started by: Casey
6 Replies

9. Shell Programming and Scripting

how to include field in the output filename of awk

Im using awk and I want the output filename to contain the first field of the input file. Ex. 1 dddd wwwww 1 eeeee wwww 1 wwww eerrrr 2 eeee eeeeee I want the output files to be xxx1 and xxx2 Thank you (4 Replies)
Discussion started by: yahyaaa
4 Replies
Login or Register to Ask a Question