views:

43

answers:

4

Hi,

I have a list of directories starting from /root. Eg

random text /root/dir1/files random end

delete me /root/dir1/files/2dir I am waste

/root/examples/source alalalala

/root/example/header some other text

I want to use sed to take from every line the directory and remove everything else. My final output must be

/root/dir1/files

/root/dir1/files/2dir

/root/examples/source

/root/example/header

How can I do it? (extract everything starting from /root... until the end of the path)

A: 

YET ANOTHER EDIT: Seems that my initial evaluation of the question did not really read it as closely as it should have. Thanks to the commentary below, I've learned a few tricks, and can clear up this answer.

First, tylerl has shown a command that should work for this in sed in this answer. Neat trick, though I'll probably stick to awk for this.

My own solution did not take into account the first couple of lines given in the description. ghostdog74 gives a solution that might work here.

Take a look at the above linked solutions.

Marc Reside
when you use awk/nawk, there is no need to use sed. `nawk '/^\/root/{print $1}'`. People often have the wrong impression that sed and awk work together, when in fact, awk is enough.
ghostdog74
Sed will do it too. As will perl. As will bash. As will python... etc. Awk isn't "what you're looking for", but rather "another tool that will work".
tylerl
@ghostdog74 That's much cleaner, very nice.@tylerl I was not aware of that functionality with sed. Upvoted your answer below, as it directly answers the OP's question.
Marc Reside
These print "random" and "delete" or nothing for the first two lines. It doesn't do what the OP wants. I don't know why it was accepted.
Dennis Williamson
I am also not sure how this answer came to be accepted. I have updated with links to the two below solutions, both of which apply more rigidly to the question above. The answer has also been Community Wiki'd.
Marc Reside
+1  A: 
awk '{for(i=1;i<=NF;i++) if ($i ~/\/root/) {print $i} }' file
ghostdog74
+2  A: 

Here you go:

sed 's|^[^/]*\(/[^ ]*\).*$|\1|'

This assumes that filenames don't have spaces in them, and the first "/" on the line marks the start of a filename.

Revise as:

sed -n 's|^[^/]*\(/[^ ]*\).*$|\1|p'

to only output lines that match the pattern (no blank lines).

tylerl
@Dennis: fixed.
tylerl
+2  A: 

For the sake of gratuitous completeness, I've also included a solution in a few other languages:

Bash

while read LINE; do
  X=${LINE#*/}
  X=${X%% *}; 
  [ "$X" == "" ] || echo /$X
done  

Perl

 perl -ne '/(\/root[^ ]+)/ and print "$1\n"'

Lua

#!lua
while true do
  local line = io.read()
  if line == nil then break end
  s = string.match(line,"/root[^%s]+")
  if s then print(s) end
end
tylerl