LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-28-2017, 03:15 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Rep: Reputation: 90
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?
 
Old 04-28-2017, 03:28 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,778

Rep: Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572
Just add "$1=$1" before your "print". Pick a field, any field ...
 
1 members found this post helpful.
Old 04-28-2017, 03:38 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
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.
 
Old 04-28-2017, 04:15 AM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by vincix View Post
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.
Old 04-28-2017, 05:19 AM   #5
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
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?
 
Old 04-28-2017, 05:33 AM   #6
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
Old 04-28-2017, 05:42 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,778

Rep: Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572
I have relied on it for years. Never failed me yet.
 
Old 04-28-2017, 07:51 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
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.
Old 04-28-2017, 08:06 AM   #9
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
Yes, that looks much cleaner. I'd prefer something more synthetic, of course. Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using awk print apss_evaluator Linux - Newbie 2 03-28-2015 04:10 AM
[SOLVED] awk print dos not print text value jozelo Linux - Newbie 2 10-23-2013 04:37 AM
[SOLVED] help with awk-print please saeedamer Programming 6 03-16-2012 12:44 PM
How do I - not print - in awk? ZimMonkey Programming 10 08-21-2009 07:11 PM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration