Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

awk - output field separator

UNIX for Dummies Questions & Answers


 
 
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 Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 628 Times in 585 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: 28 March 2016, 8:48 AM EDT
Location: Newtown, PA
Posts: 171
Thanks: 12
Thanked 49 Times in 45 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: 28 July 2016, 3:18 PM EDT
Location: Galactic Empire
Posts: 3,501
Thanks: 247
Thanked 1,264 Times in 1,186 Posts
Using AWK gsub function to substitute OFS


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

Sponsored Links
 

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
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:08 AM.