awk - output field separator | Unix Linux Forums | UNIX for Dummies Questions & Answers

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 01-06-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 July 2014, 11:07 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,663
Thanks: 182
Thanked 616 Times in 574 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 01-06-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 23 July 2014, 6:51 AM EDT
Location: Newtown, PA
Posts: 121
Thanks: 7
Thanked 28 Times in 27 Posts
Thanks radoulov! The reconstruction of the record solution was new to me.
    #4  
Old 01-06-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 July 2014, 12:06 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:10 PM.