Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

awk - output field separator

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-06-2013
locoroco locoroco is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 15 February 2014, 7:50 PM EST
Posts: 127
Thanks: 44
Thanked 3 Times in 3 Posts
awk - output field separator

In awk, how do I print all fields with a specified output field separator?

I have tried the following, which does not print the output FS:


Code:
echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}'

Sponsored Links
    #2  
Old Unix and Linux 01-06-2013
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 Posts
You'll need to either print the fields one by one or force the reconstruction of the record:

Code:
zsh-5.0.0[]% echo a b c d | awk 'BEGIN { OFS = ";" }{ print $1, $2, $3, $4 }'
a;b;c;d
zsh-5.0.0[]% echo a b c d | awk 'BEGIN { OFS = ";" } { $1 = $1; print }'     
a;b;c;d

Assigning a value to a field causes the reconstruction of the current record,
as you only need to change the output format, you may use the $n = $n idiom.
The Following 2 Users Say Thank You to radoulov For This Useful Post:
aydj (12-09-2013), mjf (01-06-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-06-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 22 May 2015, 12:58 PM EDT
Location: Newtown, PA
Posts: 151
Thanks: 9
Thanked 37 Times in 35 Posts
Thanks radoulov! The reconstruction of the record solution was new to me.
    #4  
Old Unix and Linux 01-06-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 May 2015, 10:54 AM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 Posts
Using AWK gsub function to substitute OFS


Code:
echo a b c d | awk '{gsub(OFS,";")}1'
a;b;c;d

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
field separator in awk yifangt Shell Programming and Scripting 11 01-30-2011 09:54 AM
awk, comma as field separator and text inside double quotes as a field. kevintse Shell Programming and Scripting 8 11-15-2010 05:31 PM
awk (nawk) field separator gc_sw Shell Programming and Scripting 4 11-03-2010 04:13 AM
Field separator in awk aoussenko Shell Programming and Scripting 2 03-29-2010 12:59 PM
Can't figure out what field separator to use in awk.... thom.mattson UNIX for Dummies Questions & Answers 3 06-10-2009 02:42 AM



All times are GMT -4. The time now is 10:43 PM.