Awk miscellaneous doubts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk miscellaneous doubts
# 1  
Old 01-19-2011
Awk miscellaneous doubts

Hi,

I am having the following doubts on awk. Please clarify for me.

a) What does it mean?
Code:
awk '$1=$1'

and how does it change if I append FS="" to the above code?



b) What if I use awk -vFS="\n" (i.e) setting (input) field separator to newline char, then what will be the value of $0, $1...? I guess "$0" denotes the whole record right?



c) To find the duplicate record in a file, I may use
Code:
awk 'a[$0]++ > 1 {exit 1}' file
awk '{if (found[$1] > 0) print $1 " is a dup"; ++found[$1]}' file

Both returns the same result though the validation check is different. The first one checks for "> 1" and second one checks for "> 0". This is confusing.

Last edited by royalibrahim; 01-19-2011 at 08:06 AM..
# 2  
Old 01-19-2011
That's enough to list the duplicate records.
Code:
awk 'a[$0]++' file

---------- Post updated at 09:30 PM ---------- Previous update was at 09:27 PM ----------

for first question:
Code:
$ cat infile
abc jfkjff           alkjflkajfajfk
ab
abcd
abc
aB

$ awk '$1=$1' infile
abc jfkjff alkjflkajfajfk
ab
abcd
abc
aB

$ awk '$1=$1' FS="" infile
a b c   j f k j f f                       a l k j f l k a j f a j f k
a b
a b c d
a b c
a B

# 3  
Old 01-19-2011
awk "$1=$1" is typically used to collapse extra spaces in a text file
Code:
$ echo "a    b      c      d" | awk "$1=$1"
a b c d

# 4  
Old 01-19-2011
Quote:
Originally Posted by fpmurphy
awk "$1=$1" is typically used to collapse extra spaces in a text file
Code:
$ echo "a    b      c      d" | awk "$1=$1"
a b c d

Thanks for this info, can you please illustrate on its working i.e. how it actually works.
# 5  
Old 01-19-2011
Any assignment to the positional parameters causes the input line ($0) to be rebuilt with the current OFS.

The statement $1=$1 also returns a non-null value and this causes awk to perform the {} block (or in this case as there isn't a {} block to print the value of $0).
# 6  
Old 01-20-2011
Quote:
Originally Posted by Chubler_XL
Any assignment to the positional parameters causes the input line ($0) to be rebuilt with the current OFS.

The statement $1=$1 also returns a non-null value and this causes awk to perform the {} block (or in this case as there isn't a {} block to print the value of $0).
Thank you Chubler_XL, but Is it just an assignment of positional parameter or that particular field? why it is just $1 and not $0 or $2, $3... etc.,? How this assignment collapses the extra spaces?
# 7  
Old 01-20-2011
It will not work with $0, since $0 is the entire record, not a field. $2=$2 or $7=$7 would work too, but $1=$1 is better since there isn't always a $2 and then you would be introducing empty fields.

---------- Post updated at 12:13 ---------- Previous update was at 12:09 ----------

Quote:
Originally Posted by Chubler_XL
Any assignment to the positional parameters causes the input line ($0) to be rebuilt with the current OFS.

The statement $1=$1 also returns a non-null value and this causes awk to perform the {} block (or in this case as there isn't a {} block to print the value of $0).
This is correct only if $1 is not zero or "" , otherwise those lines will not get printed... So in that case it would be safer to use:
Code:
awk '{$1=$1}1'

Quote:
Originally Posted by fpmurphy
awk "$1=$1" is typically used to collapse extra spaces in a text file
Code:
$ echo "a    b      c      d" | awk "$1=$1"
a b c d

Single quotes should be used instead of double quotes...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Doubts About awk, and Gawk

well i have some doubts about the use of this commands: my first doubt is to know if there is a way to execute a awk program from a file? (now i do copy paste, i copy the script of a notepad on the terminal and then i press enter, but i want to put this scripts in some folder and execute them)... (3 Replies)
Discussion started by: matius_88
3 Replies

2. HP-UX

Some doubts about resizing fs's in HP-UX

Hello, I'm new to HP-UX and I'm not sure about some concepts related to resizing fs's under this OS. First of all I'm only asking about resizing ONLINE, it means, without having to umount the fs nor rebooting, etc. Q1. I've read that in order to resize a fs online there are 2 requirements:... (3 Replies)
Discussion started by: asanchez
3 Replies

3. Shell Programming and Scripting

Miscellaneous operators

Hi everyone, I read some shell script code,then I have some issue. the following code. let "t1 = ((5 + 3, 7 - 1, 15 - 4))" echo "t1 = $t1" t1=11 Here t1 is set to the result of the last operation.why? (3 Replies)
Discussion started by: luoluo
3 Replies

4. Shell Programming and Scripting

awk miscellaneous doubts

In this following command: awk 'BEGIN{ORS=""}1' what does '1' signifies that comes after closing curly brace '}' of awk? I guess, it does not mean 'first occurrence' because I verified that. And, pls tell me how to override or suppress awk's field variables like $1, $2.. by positional... (7 Replies)
Discussion started by: royalibrahim
7 Replies

5. Shell Programming and Scripting

awk, sed and a shell doubts

Hi, I have 3 doubts which I posted it here. Doubt 1: I have a file containing data: 22 -73 89 10 99 21 15 -77 23 63 -80 91 -22 65 28 97 I am trying to print the fields in the reverse order and replace every field by its absolute (positive) value (ie.) I am looking for output: 10... (8 Replies)
Discussion started by: royalibrahim
8 Replies

6. UNIX for Dummies Questions & Answers

Unix doubts

Hi All, 1. how and who calls .profile when you login 2. what is PPID and what means by PPID = 0 Thanks in Advance (2 Replies)
Discussion started by: ravi.sadani19
2 Replies

7. UNIX for Dummies Questions & Answers

Doubts on FIFO

Hi , I m beginner for Unix and i want to use FIFO in my 2 Scripts . I want 1 script to read data from FIFO and other will write into FIFO. Despite reading so many articles/posts i am still unable sunchronize my scripts. My doubts are 1> Do We require both scripts as daemons to use... (0 Replies)
Discussion started by: Akshay
0 Replies

8. Shell Programming and Scripting

doubts in crontab

Hi All, I am tring to set a shedule for cron to execute my script in every 15 min, n want a mail alert on my mail id if cron fails to get data as input for my script, how can i set that? Thanks in advance Subin (1 Reply)
Discussion started by: subin_bala
1 Replies

9. UNIX for Dummies Questions & Answers

unix doubts

Write unix command to list or view auto links (0 Replies)
Discussion started by: ashishshah.engg
0 Replies
Login or Register to Ask a Question