Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-28-2017, 03:15 AM
|
#1
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Rep:
|
awk print $0 vs print $1, $2, $3, etc.
I realised that there's a clear difference between print $0 and printing all the fields in a file in awk.
For instance, if I have a file with fields separated by commas and I want to change the FS to tabs (using OFS), if I do print $0, then awk will print the whole lines as they are regardless of the FS. It just outputs the lines from the file I feed it, instead of separating the fields with tabs.
So my question is, if I have a large file with let's say 20 fields, how can I make awk print all the fields while taking into account FS and OFS? In other words, is there another way the whole lines without using $1, $2, $3, etc. other than print $0?
|
|
|
04-28-2017, 03:28 AM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,236
|
Just add "$1=$1" before your "print". Pick a field, any field ...
|
|
1 members found this post helpful.
|
04-28-2017, 03:38 AM
|
#3
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
I don't understand. Where am I supposed to add it exactly? Before the print, within the brackets? I get syntax error. If I write print $1=$3, it only prints the 3rd field.
|
|
|
04-28-2017, 04:15 AM
|
#4
|
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375
|
Quote:
Originally Posted by vincix
I don't understand. Where am I supposed to add it exactly? Before the print, within the brackets? I get syntax error. If I write print $1=$3, it only prints the 3rd field.
|
Seems you misread what you were advised.
Code:
$ echo "some random text here" | awk 'OFS="\t\t" {$1=$1; print $0}'
some random text here
$ echo "some random text here" | awk 'OFS="\t\t" {print $0}'
some random text here
Anyways, there are better facilities for formatting such as the column facility, when it comes to formatting.
Last edited by r3sistance; 04-28-2017 at 04:22 AM.
|
|
1 members found this post helpful.
|
04-28-2017, 05:19 AM
|
#5
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Right, thanks for clearing that up!
I've got another related question: what is the actual meaning of $1=$1? What is awk doing exactly by this expression?
|
|
|
04-28-2017, 05:33 AM
|
#6
|
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375
|
It is writing the value of $1 to $1, essentially it is making no difference but as a parameter was updated, it also needs to update $0. It isn't a behavior I would personally rely on.
|
|
|
04-28-2017, 05:42 AM
|
#7
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,236
|
I have relied on it for years. Never failed me yet.
|
|
|
04-28-2017, 07:51 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
Just a btw, the print is not actually required, although to a beginner can make things clearer. So for your example of a csv you could do:
Code:
awk -F, '$1=$1' OFS='\t' file
|
|
1 members found this post helpful.
|
04-28-2017, 08:06 AM
|
#9
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Yes, that looks much cleaner. I'd prefer something more synthetic, of course. Thanks.
|
|
|
All times are GMT -5. The time now is 04:13 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|