Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-09-2013, 01:36 AM   #1
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, 02:15 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,681

Rep: Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856Reputation: 2856
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, 04:29 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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, 04:41 AM   #4
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,707

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
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.


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

All times are GMT -5. The time now is 11:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration