LinuxQuestions.org
Visit Jeremy's Blog.
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 11-09-2013, 02:36 AM   #1
chingstah
LQ Newbie
 
Registered: Nov 2013
Posts: 1

Rep: Reputation: Disabled
sed and awk pipes (command line)


Recently ran into a problem during one of my homework assignments regarding sed/awk that really stumped me. Sorry if it's a little long, any advice or partial solutions would help me greatly. Here is the question:

Suppose that -l was the only option available for the ls utility and that the sort utility had no options available at all. Consider generating a long listing sorted by owner (3rd column). Type a single command-line that includes sed and/or awk to produce a long listing sorted by owner (that is, fed by an "ls -l" command-line pipe). Don't use any options other than "-l" to the "ls" command and none at all for the sort command. BIG HINT: To do this, first process the long listing of the current working directory so that the owner of each file (column 3) is duplicated at the front of each line (being careful to do this in such a way that you can remove it again, later in the pipe). Further, pipe these results to a process to delete the first line (the header) of the listing, sort the results (which will be sorted by owner as you duplicated that column at the front of each line), and finally, remove the owner column (that you previously added to the front of each line). ANOTHER HINT: you can assume that all user names consist only of lower-case letters, but you don't know their individual widths). Your final result will be a long listing sorted by owner. Be sure not to somehow end up with any extra space at the front of each line (the original ls -l listing did not contain any leading spaces)! What you end up with should look EXACTLY like the ORIGINAL long listing, except sorted by owner. Make sure the column widths are not damaged (don't produce ragged columns, and you can't assume that all data in every column will just happen to have the same width... for example, the owner names could be of different lengths - I mention this so that you can make sure that what works for you will also work for me). You might have to make some fake data with different owners in the 3rd column to practice with before you report your final solution. Make sure your final output doesn't look like it has been re-formatted (the columns should all line up properly, as in the original)."
 
Old 11-09-2013, 03:15 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,119

Rep: Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269
sorry guy, I do not really understand what do you expect now.
Do you have any part implemented? Usually in such cases I will not use piped chain of tools but one single script (awk, perl or whatever you prefer)
 
Old 11-09-2013, 05:29 AM   #3
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
I am with pan64, what have you done and exactly where are you stuck?
Hints aside, the question looks relatively straight forward.

I would add that whilst the column referred to is in a common place, I would personally not play with the output of ls generally as it is not always in the exact same format.
 
Old 11-09-2013, 05:41 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
And there really is no need to even use sed...

The hint was fairly complete... and if you read anything about awk you would know that the $0 field is the entire line... all that is left is what to use as a separator between the sort key and the original line.
 
  


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
get only first line using sed / awk iffarrukh Programming 3 05-24-2012 07:13 PM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
filter source line based on results line in log using awk and sed samanp Programming 5 04-06-2011 10:42 AM
Simplify awk command by reducing pipes keysorsoze Linux - Software 5 10-03-2009 10:19 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 08:07 AM


All times are GMT -5. The time now is 06:25 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