LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2013, 02:33 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
mighty mighty TR command


Mighty mighty 'tr' vs 'awk' for list layout


I am all about using the right tool for the job - awk works great.

Code:
icasper@casperbox01s:/production/log/mo> cat /tmp/fix_pat | awk '{print $3}' | sort -u
roker1
broker2
Energy
external1
external2
trader1
trader2

however sometimes i want to layout the list so i can cut and paste it into a for loop - i use tr, which is a little aggravating because
the command prompt is appended to the end of the tr list - but besides that, tr "\n" works.

Code:
casper@casper:/production/log/mo> cat /tmp/fix_pat | awk '{print $3}' | sort -u | tr "\n" " "
broker1 broker2 Energy external1 external2 trader1 trader2 casper@casperbox:/production/log/mo>
however is there an awkish way to do improve the layout?

Last edited by casperdaghost; 10-25-2013 at 02:34 PM.
 
Old 10-25-2013, 03:03 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
hmmm ... you need to be careful with what you are saying here:
Quote:
however sometimes i want to layout the list so i can cut and paste it into a for loop - i use tr, which is a little aggravating because
the command prompt is appended to the end of the tr list
No it is not. You have said to change all newlines for spaces, you can equate this to using printf in awk and not placing a newline in the print string:
Code:
awk '{printf "%s ",$1}' file
This does not append the command prompt either. What is happening is at the completion of the command you have executed you are returned to the command prompt.
As the last thing executed by your command did not insert a new line character, the prompt is now immediately after the last string printed.

As to your requirement, using the correct tool also comes with the idea that using the tool as intended is also followed:
Code:
awk '{_[tolower($3)]=$3}END{asorti(_,a);for(i in a)print _[a[i]]}' ORS=" " /tmp/fix_pat
Notes:

1. If you wish to have the last space removed you will need to process the last print command differently (leave that for you to work out)

2. awk can read a file (no need for cat), has a sort function (no need for sort) and has the ability to only show unique values (no need for uniq or -u for sort)
 
Old 10-25-2013, 06:06 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,840

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
Nice post grail - however one also needs to be careful using "awk" generically. Lots of the features we come to rely on are GNU specific - some of the older Unices (still found in devices such as some VTLs) have awk implementations that are woefully inadequate.
Ask me how I know ...
 
Old 10-26-2013, 04:17 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Good point syg00
 
  


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
Mighty mouse trackball clarusthedogcow Linux - Hardware 4 06-09-2009 01:24 PM
Mighty mouse trackball clarusthedogcow Linux - Hardware 1 05-05-2008 10:31 PM
Mighty Slackware has struck out? WilliamS Slackware 21 09-30-2007 06:50 PM
The Apply Mighty Mouse adriaanbw Linux - Hardware 3 08-04-2005 08:58 PM
Microsoft high and mighty JonChristmas Linux - General 3 06-17-2004 09:22 PM


All times are GMT -5. The time now is 05:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration