awk prints only last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk prints only last line
# 1  
Old 03-25-2016
awk prints only last line

data.txt:

Code:
NEWTEXTS="frq=63,std=-0.00533584,time=Mar-21-(09:15:03)-2016,epoch=1458576903,avg=64.2059,212.698
frq=197,std=0.587585,time=Mar-21-(09:16:02)-2016,epoch=1458576962,avg=64.2059,483.756
frq=178,std=0.503514,time=Mar-21-(09:46:02)-2016,epoch=1458578762,avg=64.2059,500
frq=0,std=-0.284097,time=Mar-21-(10:12:01)-2016,epoch=1458580321,avg=64.2059,246
frq=145,std=0.357496,time=Mar-21-(10:13:01)-2016,epoch=1458580381,avg=64.2059,514.483
frq=178,std=0.503514,time=Mar-21-(10:16:01)-2016,epoch=1458580561,avg=64.2059,510.112
frq=84,std=0.0875845,time=Mar-21-(10:46:02)-2016,epoch=1458582362,avg=64.2059,1098.81
frq=63,std=-0.00533584,time=Mar-21-(10:55:02)-2016,epoch=1458582902,avg=64.2059,1192.06
frq=180,std=0.512363,time=Mar-21-(11:15:02)-2016,epoch=1458584102,avg=64.2059,528.333
frq=63,std=-0.00533584,time=Mar-21-(11:34:03)-2016,epoch=1458585243,avg=64.2059,422.222
frq=329,std=1.17166,time=Mar-21-(11:35:10)-2016,epoch=1458585310,avg=64.2059,237.082
frq=63,std=-0.00533584,time=Mar-21-(11:45:03)-2016,epoch=1458585903,avg=64.2059,252.381
frq=222,std=0.698204,time=Mar-21-(11:46:01)-2016,epoch=1458585961,avg=64.2059,470.721
frq=3,std=-0.270823,time=Mar-21-(12:11:01)-2016,epoch=1458587461,avg=64.2059,4800
frq=147,std=0.366346,time=Mar-21-(12:12:01)-2016,epoch=1458587521,avg=64.2059,586.395
frq=182,std=0.521213,time=Mar-21-(12:16:01)-2016,epoch=1458587761,avg=64.2059,491.209
frq=179,std=0.507938,time=Mar-21-(12:31:02)-2016,epoch=1458588662,avg=64.2059,565.363"

awk code:

Code:
echo "${NEWTEXTS}" | awk -F, '{a=$1; b=$2; c=$3; d=$4; e=$5; f=$6 } END{printf("%s,%s,%s,%s,%s,%0.f\n", a,b,c,d,e,f)}'

this awk code only prints the last line. but i want it to print all the lines but with the last field of each line rounded up.

any ideas?
# 2  
Old 03-25-2016
Hello SkySmart,

Could you please try following and let me know if this helps.
Code:
echo "${NEWTEXTS}" | awk -F, '{a=$1; b=$2; c=$3; d=$4; e=$5; f=$6 } {printf("%s,%s,%s,%s,%s,%0.f\n", a,b,c,d,e,f)}'

Off course culprit there was END in your code. Also you could go through the manual page of awk by doing man awk, as follows mentioned in that for END.
Quote:
BEGIN and END are two special kinds of patterns which are not tested against the input. The action parts of all BEGIN patterns are merged as if all the statements had been written in a single BEGIN block. They are executed before any of the input is read. Similarly, all the END blocks are merged, and executed when all the input is exhausted (or when an exit statement is executed). BEGIN and END patterns cannot be combined with other patterns in pattern expressions. BEGIN and END patterns cannot have missing action parts.
Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-25-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

Could you please try following and let me know if this helps.
Code:
echo "${NEWTEXTS}" | awk -F, '{a=$1; b=$2; c=$3; d=$4; e=$5; f=$6 } {printf("%s,%s,%s,%s,%s,%0.f\n", a,b,c,d,e,f)}'

Off course culprit there was END in your code. Also you could go through the manual page of awk by doing man awk, as follows mentioned in that for END.

Hope this helps.

Thanks,
R. Singh

Your suggestion works!

Thank you!
# 4  
Old 03-25-2016
As per the requirements, your current solution does not always round up the last field. The below solution will always round up the last field:

Code:
echo "${NEWTEXTS}" | awk '$NF=$NF==int($NF)?int($NF):int($NF)+1' OFS=, FS=,

This User Gave Thanks to pilnet101 For This Post:
# 5  
Old 03-25-2016
Quote:
Originally Posted by pilnet101
As per the requirements, your current solution does not always round up the last field. The below solution will always round up the last field:

Code:
echo "${NEWTEXTS}" | awk '$NF=$NF==int($NF)?int($NF):int($NF)+1' OFS=, FS=,

it works. thank you!
care to explain how the awk is working?
# 6  
Old 03-25-2016
Sure,

So this part below will re-evaluate the value of $NF for every line read. The value of $NF being the last field.
Code:
awk '$NF=


For this part, ideally it should be enclosed in brackets. This is what the value of $NF is being set to. It is using the conditional (ternary) operators ?: to set the value of $NF to int($NF) if $NF is already equal to int($NF), so basically if it is already an integer and not a decimal, don't change it. If $NF is not equal to int($NF), i.e. Is a decimal, then set $NF to int($NF)+1. The int function is essentially rounding down the number.
Code:
$NF==int($NF)?int($NF):int($NF)+1'


This will set both the field separator (FS), same as doing - F, and the output field separator (OFS) to a comma.
Code:
OFS=, FS=,

And lastly, awk will perform it's default action of printing the lines it performs the operation on.
This User Gave Thanks to pilnet101 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

Awk: BEGIN: prints nothing

My code fails to do anything if I've BEGIN block in it: Run the awk script as: awk -f ~/bin/sum_dupli_gene.awk make_gene_probe.txt #!/usr/bin/awk -f BEGIN { print ARGV #--loads of stuff } END{ #more stuff } (14 Replies)
Discussion started by: genome
14 Replies

2. Shell Programming and Scripting

awk prints unwanted new lines

Hello, I need a little help with the following: I'm using AWK to read input from a comma-seperated value file, and only printing certain fields like so: awk -F "," '{print $1,$3,$6}' /list.csv | tail -1 Which outputs the following: server1 APPID OS I run into a problem... (8 Replies)
Discussion started by: LinuxRacr
8 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

prints some fields from different files into a line of new file

i have 3 files as below: i want to print 1st,2nd,5th and 10th filed of 1st to 5th lines from each files into a line of an output file, so the result would be: : {line1}(field 1 of line 1 from file 1)(field 2 of line 1 from file 1)(field 5 of line 1 from file 1)(field 10 of line 1 from file... (1 Reply)
Discussion started by: saeed.soltani
1 Replies

5. Red Hat

hostname command prints nothing a null value

Hi , On my box everything works fine. But whenever I run command It returns nothing as you see @(none) too. Its very strange issue I have never noticed on any other system yet. Any one have any idea about this. Thank you (2 Replies)
Discussion started by: pratapsingh
2 Replies

6. Shell Programming and Scripting

awk system() prints extra 0 -- why?

Here's the command I'm running: # echo "hi" | awk '{etime = system("hostname") ; close("hostname") ; print etime""}' And here's the ouput: server.domain.tld 0 Why in the world is that second line, the one that's just "0", there? Many thanks in advance. (2 Replies)
Discussion started by: treesloth
2 Replies

7. Solaris

Epson ESC/P2 on solaris prints LD

I am hoping someone can help me with this little riddle. I have an oldish Epson ESC/P2 which I connected to a solaris10 box, with 2 queues, 1 with the solaris print filter so I can print from Solaris (works fine), and a second raw queue so that windows machines can print to it. The raw queue... (0 Replies)
Discussion started by: gdommett
0 Replies

8. Programming

Core image showing no prints

Hi All, I am facing a very peculiar problem with my applciation. The problem is that one of my process is making core very rarely.But when I am going to debug it,it is not showing any prints. The Make file is : g++ -g $(commonobjfiles) -o ipcl $(INCLUDES) -z muldefs -lpthread -lsocket... (0 Replies)
Discussion started by: unisuraj
0 Replies

9. Shell Programming and Scripting

awk / grep / Prints all columns except the first one

I have the a file with the following data. File Content. 1249 snf06.3.0 sw version 1.1 code MD5 192F MD4 MD3 1248 sns06.3.0 sw version 1.1 code MD5 192F MD12 1250 sns06.3.0 sw version 1.1 code MD5 192F0\ MD8 1241 sns06.3.0 sw code MD5 19 1243 sn06.3.0 sw version 1.1 code MD5 19 12... (17 Replies)
Discussion started by: knijjar
17 Replies

10. Solaris

Screen prints on SunOS 5.7

Is there an easy way to print what is on the screen? Running SunOS 5.7 and blackbox. (1 Reply)
Discussion started by: grayson
1 Replies
Login or Register to Ask a Question