figuring out an awk one liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting figuring out an awk one liner
# 1  
Old 05-15-2012
figuring out an awk one liner

I have googled around a bit and could not find an answer to how this works:
Code:
echo $STRING | awk '$0=$NF' FS=

I know what each part is doing. The record is being set to equal the last field and the field separator is being set to null so that each character is considered a field. Why can FS= be put at the end like that though? I presume that the '$0=$NF' is used as a pattern match. Perhaps my problem is more with the syntax as I am used to using awk more like:
Code:
   awk '/string/ { # some code etc. }'

Thanks,
Ben

Last edited by Scrutinizer; 05-15-2012 at 01:34 PM.. Reason: code tags
# 2  
Old 05-15-2012
Not as a pattern match, it prints the last character if it is unequal to "" or 0. Everything in awk has the form condition{action}. If the condition evaluates to 1 then the action is performed. If the action is omitted then the default action is performed, which is {print $0}.
So this is equivalent to:
Code:
awk '$0=$NF{print $0}' FS=

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-15-2012
That's an old-fasioned way of setting variables in awk. Setting a blank field-separator means, in some versions of awk, to split on every single character.

So what it's actually doing, is setting the entire line($0) to the last field($NF), and printing if it's anything but blank.

So I'd expect this to just print the last character of a line all the time.

I don't think this is a very efficient usage. Never use awk to process one single line if you can help it, that's lighting a furnace to burn a hair; modern enough shells have builtins which can do this without the overhead of running an entire process.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 05-15-2012
Thanks. I realize the result. I am trying to work out why. I know that $0 is being set to the last character because the field separator has been set to null and $0 is being set to the last field. But I am not used to the way it has been written (i.e. without {...} etc.). I presume it is outputting each record by default. I am still a little confused about this one liner. Any further clarification would be appreciated.

Thanks,
Ben

---------- Post updated at 10:51 AM ---------- Previous update was at 10:43 AM ----------

Quote:
Originally Posted by Corona688
That's an old-fasioned way of setting variables in awk. Setting a blank field-separator means, in some versions of awk, to split on every single character.
Ah. That was the main part which was puzzling me. Is it handled like doing a -vFS= and can this method of variable assignment be relied on for different platforms?

Thanks,
Ben

---------- Post updated at 11:01 AM ---------- Previous update was at 10:51 AM ----------

Quote:
Originally Posted by Corona688
I don't think this is a very efficient usage. Never use awk to process one single line if you can help it, that's lighting a furnace to burn a hair; modern enough shells have builtins which can do this without the overhead of running an entire process.
I agree. I know I can do this: echo ${STRING#${STRING%?}} or in ksh I can do: typeset -R1 c=$STRING; echo $c. Coming across this line in awk that I couldn't completely understand though bothered me.

Ben
# 5  
Old 05-15-2012
Quote:
Originally Posted by benalt
"...Is it handled like doing a -vFS= and can this method of variable assignment be relied on for different platforms?
This method of assigning variables can be relied upon, and I think it is more convenient than -v, but note that the variable will not be available in the BEGIN section. The special meaning of setting FS to an empty string, does not work in every awk..
This User Gave Thanks to Scrutinizer 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 one liner

The below code is a simple modified sample from a file with millions of lines containing hundreds of extra columns xxx="yyy" ... <app addr="1.2.3.4" rem="1000" type="aaa" srv="server1" usr="user1"/> <app usr="user2" srv="server2" rem="1001" type="aab" addr="1.2.3.5"/>What's the most efficient awk... (2 Replies)
Discussion started by: cabrao
2 Replies

2. Shell Programming and Scripting

Combine these two into one liner awk?

ignore the simplicity of the foo file, my actual file is much more hardcore but this should give you the jist of it. need to combine the two awks into one liner. essentially, need to return the value of one particular field in a file that has multiple comma separated fields. thanks guys cat foo... (1 Reply)
Discussion started by: jack.bauer
1 Replies

3. UNIX for Dummies Questions & Answers

awk or sed one liner

I have a data base of part numbers: AAA Thing1 BBB Thing2 CCC Thing3 File one is a list of part numbers: XXXX AAA234 XXXX BBB678 XXXX CCC2345 Is there a sed one-line that would compare a data base with and replace the part numbers so that the output looks like this? XXXX AAA234... (7 Replies)
Discussion started by: jimmyf
7 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

6. Shell Programming and Scripting

Awk one-liner?

Hello, I have two files... File #1 1 3 2 5 File #2 3 5 3 1 3 7 9 1 5 2 5 8 3 3 1 I need to extract all lines from File #2 where the first two columns match each line of File #1. So in the example, the output would be: 1 3 7 2 5 8 Is there a quick one-liner that would... (4 Replies)
Discussion started by: palex
4 Replies

7. UNIX for Dummies Questions & Answers

need an awk one liner

example input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

8. Shell Programming and Scripting

grep-awk one liner help

Hi guys, I'm trying to create a one line command that does the following. I will post my command first so you can get the idea better: ls -larht | awk '{print $4}' | uniq | xargs grep * __________ ls -larht | awk '{print $4}' | uniq This will post the name of the groups of each file... (2 Replies)
Discussion started by: erick_tuk
2 Replies

9. UNIX for Dummies Questions & Answers

awk one liner

I need a one liner to" find /pattern/ print from x lines before "pattern" to y lines after "pattern" (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

10. Shell Programming and Scripting

awk one liner

input a 100 200 300 b 400 10 output a 100 a 200 a 300 b 400 b 10 Thanx (6 Replies)
Discussion started by: repinementer
6 Replies
Login or Register to Ask a Question