Explanation of print statement - awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explanation of print statement - awk
# 1  
Old 01-31-2013
Explanation of print statement - awk

Hi, i'm just after a simple explanation of how the following awk oneliner works.
Code:
 
gawk -F"," '{for(i=m;i<=n;i++)printf "%s" OFS,$i; print x}' m=1 n=70 OFS=, input.csv > output.csv

In particular i'm trying to understand how the two print statements work? How is the "x" variable being assigned a value? Is the "x" variable being assigned the value of the first printf statement?

Thanks in advance,
TheFlamingMoe
# 2  
Old 01-31-2013
I can see why you're puzzled, that took some thought.

Nothing at all is assigned to X. It's printing a newline. print "" would have the same effect, but they saved one character by using a blank variable. Just print all by itself with no parameters would print the entire line, not what you want.

The printf statements do not print newlines, those must be explicit when using printf. So it prints a bunch of things into one long line, then uses print "" to finish off the line.

Are you sure that's OFS,$i and not OFS $i ? Two things into a single %s is a syntax error as far as I know.
# 3  
Old 02-01-2013
Thanks Corona688, you answered my question perfectly. The comma in the statement "OFS,$i" was in the original script, but it's probably a typo. I'll check it out. Thanks again Smilie

TheFlamingMoe

---------- Post updated at 05:23 AM ---------- Previous update was at 05:08 AM ----------

Quote:
Are you sure that's OFS,$i and not OFS $i ? Two things into a single %s is a syntax error as far as I know.
Checked out the above query. The awk statement doesn't work without the comma. Add the comma and things work perfectly. So I guess it's not a typo!!!
# 4  
Old 02-01-2013
Quote:
Originally Posted by Corona688
I can see why you're puzzled, that took some thought.

Nothing at all is assigned to X. It's printing a newline. print "" would have the same effect, but they saved one character by using a blank variable. Just print all by itself with no parameters would print the entire line, not what you want.

The printf statements do not print newlines, those must be explicit when using printf. So it prints a bunch of things into one long line, then uses print "" to finish off the line.

Are you sure that's OFS,$i and not OFS $i ? Two things into a single %s is a syntax error as far as I know.
It isn't a syntax error, but it isn't common. The awk printf command:
Code:
printf "%s" OFS,$i

uses the concatenation of "%s" and OFS as a format string; together they print the contents of the ith input field followed by the output field separator. If there is a bug here, it is that there is an extraneous trailing field separator printed at the end of each output line before the <newline> added by the print x command.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. UNIX for Dummies Questions & Answers

If statement explanation

Could anyone please advise what the in the following if statement means? if ; then Thank you. (2 Replies)
Discussion started by: jimbojames
2 Replies

3. Shell Programming and Scripting

Awk find in columns with "if then" statement and print results

I have a file1.txt file1.txt F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true" F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true" U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"... (4 Replies)
Discussion started by: charles33
4 Replies

4. Shell Programming and Scripting

use awk print statement for two files

Hello, is there a way to use the awk print statement on two files at once? I would like to take two columns from one file, and one column from another file and print them as consecutive columns to a third file. Seems simple...as in: file 1 1 a 2 b 3 c 4 d 5 e file 2 1 t 2 u... (3 Replies)
Discussion started by: HugoHuhn
3 Replies

5. Shell Programming and Scripting

Sysdate inside awk print statement

Hi, I am using awk statement to extract data from a file and write a new file with certain columns rearranged and few hard coded values added to new file. Now i need to add a column with sysdate. can i do that inside the awk print statement? Now: nawk ' /^3/ BEGIN {FS=","}... (2 Replies)
Discussion started by: selvankj
2 Replies

6. Shell Programming and Scripting

Help with explanation of awk parameters

Hello, Would someone be able to tell me exactly how this command works please? awk '!x++' As usual any help much appreciated (8 Replies)
Discussion started by: Grueben
8 Replies

7. Shell Programming and Scripting

awk explanation

Hello, I have recently come across this awk program. Can some one shed some light on what is taking place. awk '{!a++}END{for(i in a) if ( a >10 ) print a,i }' $FILE Best Regards, jaysunn (1 Reply)
Discussion started by: jaysunn
1 Replies

8. Shell Programming and Scripting

Explanation for printf string in awk

hi all can any one help me to understand this bdf -t vfxs | awk '/\//{printf("%-30s%-10s%-10s%-10s%-5s%-10s\n",$1,$2,$3,$4,$5,$6)}' i want to understand the numbers %-30S% (4 Replies)
Discussion started by: maxim42
4 Replies

9. Shell Programming and Scripting

Print to 2 files in awk if statement

Hi all, I have some code like this awk -F, '{ if ($1==3) print $2 > "output_file" print "1" > "new_file" }' "input_file" When I check output_file this has the correct values in it. However the new_file has 1 in it for every line in the input_file. If the input file has 20 lins then... (2 Replies)
Discussion started by: Donkey25
2 Replies

10. Shell Programming and Scripting

AWK explanation

Hi, Could anyone please explain why we have arr=1 - what does this statement do? awk -F\; 'FNR==NR{arr=1;next};$3 in arr' core.txt gmrd.txt Any help appreciated (2 Replies)
Discussion started by: penfold
2 Replies
Login or Register to Ask a Question