Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: awk and the Field Separator Variable

Status
Not open for further replies.

goldenradium2001

Technical User
Mar 22, 2002
91
US
Any help will be appreciated. I'm learning awk right now and wanted to clear up a few things.

I have a file that looks like this (the <TAB> actually indicates that I'm using Tab spaces):

one<TAB>two<TAB><TAB>three

I issue the following command to put a dash between these columns:

nawk '{print $1&quot;-&quot;$2&quot;-&quot;$3&quot;-&quot;$4}' FS=&quot;\t&quot; filename

The output that I get is:

one-two--three

My question is: Why did I have to specify four fields in order to get this output? Is the space between the two dashes -- actually a field?

When I specify only three fields using the following command:

nawk '{print $1&quot;-&quot;$2&quot;-&quot;$3}' FS=&quot;\t&quot; filename

I get the following output:

one-two-

Is this proof that there exists a field between the two -- dashes?

I know I can use:

nawk '{print $1&quot;-&quot;$2&quot;-&quot;$3}' FS=&quot;\t+&quot; filename

to get around this dilemma of having two dashes but the reason why I'm writing is because I want to know WHY the space between the two dashes -- is considered a field?

Thanks for enlightening me!!! Any guesses are welcome!



 
All right, I'm going to answer my own question. Yes, if there two instances of a field separator then there really is a field between the two field separator however it's an empty string.

Hope this helps someone.
 
The field separator is a regular expression. It could match several characters. So if you want all consecutive <TAB>s to count as only separator, this should work:
Code:
FS=&quot;\t+&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top