LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2006, 04:07 AM   #1
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Rep: Reputation: 0
A problem about awk


now i have met a problem about awk:
there is a file named "list" whose contents as follows:

Quote:
John Daggett, 341 King Road, Plymouth, Massachusetts
Alice Ford, 22 East Broadway, Richmond, Virginia
Orville Thomas, 11345 Oak Bridge Road, Tulsa, Oklahoma
now i use awk command like this:
awk -F, '{print $4 "," $2}' list

I have thought the result should be:
Quote:
Massachusetts,341 King Road
Virginia,22 East Broadway
Oklahoma,11345 Oak Bridge Road
but displayed in screen is :
Quote:
, 341 King Road
, 22 East Broadway
, 11345 Oak Bridge Road
where is the column 4 i want to display?

if i use command like this:
awk -F, '{print $4 "\t\t" $2}' list

the answer is :
Quote:
Massachusetts 341 King Road
Virginia 22 East Broadway
Oklahoma 11345 Oak Bridge Road
rhe result is normal,why,can't the symbol ',' be as a delimiter?
 
Old 07-13-2006, 04:34 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401
Hi,

The output from both awk commands is incorrect. The second one (with "\t\t") should have tabs (2 times) between the selected fields, which is not the case. The awk commands given are syntactically correct.

I do wonder if awk is the problem.

Just a few questions to clear things up a bit:

1) Is this awk command part of a script, or run as a one-liner from the command line?

2) Is this done on a linux or a unix box?

3) Is the infile (list) an ASCII text file? (file list to check),

4) Does this work: awk 'BEGIN { FS = "," } {print $4 "," $2}' list?

5) Are the shell's input/output file seperator edited (echo $IFS | od -c and the same for $OFS). Both should give you:
0000000 \n
0000001

Hope this gets us anywhere.
 
Old 07-13-2006, 05:29 AM   #3
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,druuna
now i give you the answer of yours as follows:
1) This awk command runs as a one-liner from the command line.
2) This is done on a linux desktop machine.
3) list: ASCII text, with CRLF line terminators
4) the result is the same as that of command:
awk -F, '{print $4 "," $2}' list
but "awk -F, '{print $2 "," $4}' list" works normly.
Quote:
341 King Road, Massachusetts
22 East Broadway, Virginia
11345 Oak Bridge Road, Oklahoma
and "awk -F, '{print $4 "\t\t" $2}' list" works as expected.but there can't display the symbol tab
Quote:
Massachusetts 341 King Road
Virginia 22 East Broadway
Oklahoma 11345 Oak Bridge Road
5) Both the result is right as the same as you have told me.
0000000 \n
0000001

could you help me explain why this would happend?
 
Old 07-13-2006, 05:50 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401
Hi,

The input file cannot be handled correctly (it probably is a DOS/WINDOWS) file. The lines end with ^M (ASCII text, with CRLF) instead of a plain LF.

You can change this by doing the following (just one way):

Open the file in vi and type the following (in command mode):

:set ff=unix

Safe the file and all should be well.

Or, using tr:

tr '\r' ' ' < list > list.new

Hope this helps.
 
Old 07-13-2006, 08:56 PM   #5
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Original Poster
Rep: Reputation: 0
oh,thanks a lot.
That's right now when i have do something as you have told me.

Quote:
Originally Posted by druuna
Hi,

The input file cannot be handled correctly (it probably is a DOS/WINDOWS) file. The lines end with ^M (ASCII text, with CRLF) instead of a plain LF.

You can change this by doing the following (just one way):

Open the file in vi and type the following (in command mode):

:set ff=unix

Safe the file and all should be well.

Or, using tr:

tr '\r' ' ' < list > list.new

Hope this helps.
 
  


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
Awk newline problem CelticSoul Linux - General 5 05-03-2006 09:00 PM
AWK problem embsupafly Programming 13 08-21-2005 07:43 PM
awk problem jpostma Programming 7 10-22-2003 06:07 AM
sed/awk problem player_2 Programming 9 08-26-2003 06:09 PM
awk problem alaios Programming 4 05-04-2003 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 AM.

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