awk set $1 to itself, effect on $0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk set $1 to itself, effect on $0
# 1  
Old 04-28-2013
awk set $1 to itself, effect on $0

jotne posted an interesting thread an hour or two ago, and ended up with the following:
Code:
awk '{$1=$1} /^[0-9]+$/' file

I had a question about the effect of $1=$1 assignment, and thought it better to start a new thread, because it's really a different topic.
Code:
$ cat test.sh
set -v
echo " abcd" | awk '{        print ":" $0 ":" }'
echo " abcd" | awk '{        print ":" $1 ":" }'
echo " abcd" | awk '{ $0=$0; print ":" $0 ":" }'
echo " abcd" | awk '{ $0=$0; print ":" $1 ":" }'
echo " abcd" | awk '{ $1=$1; print ":" $1 ":" }'
echo " abcd" | awk '{ $0=$1; print ":" $0 ":" }'
echo " abcd" | awk '{ $0=$1; print ":" $1 ":" }'
echo " abcd" | awk '{ $1=$0; print ":" $0 ":" }'
echo " abcd" | awk '{ $1=$0; print ":" $1 ":" }'
echo " abcd" | awk '{ $1=$1; print ":" $0 ":" }'

Code:
$ ./test.sh
echo " abcd" | awk '{        print ":" $0 ":" }'
: abcd:
echo " abcd" | awk '{        print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $0=$0; print ":" $0 ":" }'
: abcd:
echo " abcd" | awk '{ $0=$0; print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $1=$1; print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $0=$1; print ":" $0 ":" }'
:abcd:
echo " abcd" | awk '{ $0=$1; print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $1=$0; print ":" $0 ":" }'
: abcd:
echo " abcd" | awk '{ $1=$0; print ":" $1 ":" }'
: abcd:
echo " abcd" | awk '{ $1=$1; print ":" $0 ":" }'
:abcd:

Each one makes perfect sense to me, except for the last one. Could someone explain why $1=$1 results in blanks being removed from $0 var? What is going on behind the scene?

I'm sure this has been discussed before, but I could not find it on searching the forum archives. It disallowed the word "itself". Smilie
# 2  
Old 04-28-2013
That's used to rebuild the input record $0.
The following manual entry (for gawk) should make it crystal clear.
Changing Fields - The GNU Awk User's Guide
# 3  
Old 04-28-2013
I had read the documentation before. It did not make it "crystal clear" to me. Of course, that's probably my fault. I'm sure all that documentation is easy to understand for everyone else. Smilie I understood about $0 being rebuilt. Now that I think about it more, it makes sense that when $0 is rebuilt, the only input is $1 so the new version of $0 has no blanks. Thanks.
# 4  
Old 04-28-2013
$0 is rebuilt and the default FS value (a single space) is special in the sense that not only is stands for any spacing, but also leading spacing and closing spacing does not count as a field separator, but gets ignored..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to set decimal place in awk?

Dear all, I have a data test.txt as below. X22.30799720_T cg03868770 -0.5645412582127 2.4084685750406e-175 X22.30781182_A cg03868770 -0.5620426397492 3.5818034129169e-172 X22.30780724_C cg03868770 -0.5616890165605 2.9765569717858e-168 what I want is: X22.30799720_T cg03868770... (3 Replies)
Discussion started by: forevertl
3 Replies

2. Shell Programming and Scripting

Alias has no effect in script

It doesn't have effect in script but it works on the terminal root@server:/opt/kvm/usecases/logs# alias echo='echo -e' root@server:/opt/kvm/usecases/logs# echo "xxxx\n" xxxx root@server:/opt/kvm/usecases/logs# cat xx.sh #!/bin/bash alias echo='echo -n' echo "sssf \n" ... (3 Replies)
Discussion started by: yanglei_fage
3 Replies

3. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

4. UNIX for Dummies Questions & Answers

What crontab is in effect after reboot

A basic cron question and I hope I explain it enough. If a person creates a file to use with cron and issues the followin command: crontab I understand this will set the cron entries to whatever is in . However, if we now edit the crontab with: crontab -e and save, how does cron... (3 Replies)
Discussion started by: niswonp
3 Replies

5. Shell Programming and Scripting

how to give dictionary effect ?

Hello, In google: if we type the text like :- It gives us a question saying :- I want to know how to write a shell script to give this Dictionary effect. example:If we give "lst" then it corrects us saying "list". Can you please help me with a sample code! friends..... (3 Replies)
Discussion started by: nsharath
3 Replies

6. Virtualization and Cloud Computing

The Network Effect - Part 1

2008-10-31T22:46:14+01:00 http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef010535ce543e970c-800wi Nicholas Carr (and here) has some problems with Tim O'Reilly's theory about the cloud and the network effect. http://feeds.feedburner.com/~f/GeekAndPoke?i=OFn0M... (0 Replies)
Discussion started by: Linux Bot
0 Replies

7. UNIX for Dummies Questions & Answers

How do properties effect script?

Hi, I have noticed that rm -if will perform completely different to rm -fi. Whats the pattern of how I put my options to the script in relation to how it will act. i.e rm -fi treat the remove as interative but rm -if treats it as forced Thansk, Chris. (1 Reply)
Discussion started by: Chiefos
1 Replies

8. UNIX for Advanced & Expert Users

Effect of Preemptive Kernel

hi there, i am porting kernel 2.2 driver program to kernel 2.6. for some extent i am successfull but some times the system gets hanged. what might be the problem? i am not able to get any help from log messages as nothing is being printed at that moment. hey does this kernel preemptiveness and... (2 Replies)
Discussion started by: sriram.ec
2 Replies

9. UNIX for Dummies Questions & Answers

recursive effect!!

I run the following command in some of my folders... and ended up with a huge mess!! find . -type f -exec perl -e 's/blabla/zzzxxxx/gi' -p -i.bak {} \; I had to kill the process and later when I checked with one of my folders.. ls vaditerm.dt.bak vaditerm.dt.bak.bak... (2 Replies)
Discussion started by: sskb
2 Replies
Login or Register to Ask a Question