0

I have written a line that finds and returns the full path to a desired file. The output is as follows:

/home/ke/Desktop/b/o/r/files.txt:am.torrent
/home/ke/Desktop/y/u/n/u/s/files.txt:asd.torrent

I have to modify the output like this:

bor
yunus

How do I do that?

Thanks in advance.

4 Answers 4

1

This should work for you:

your_script.sh | sed 's,.*Desktop,,' | sed 's,[^/]*$,,' | sed s,/,,g

or, even better:

your_script.sh | sed 's,.*Desktop,,;s,[^/]*$,,;s,/,,g'
Sign up to request clarification or add additional context in comments.

3 Comments

Three instances of sed? Don't you think that's overkill?
No, I don't think it's an overkill, it will work fast. But yes, it can be done in one invocation of sed as well.
You can run multiple sed commands in a single instance by separating the commands with ;, like this: sed -e 's,.*Desktop,,; s,[^/]*$,,; s,/,,g'
0

With sed. echo '/home/ke/Desktop/b/o/r/files.txt:am.torrent' | sed -e 's+/++g' -e 's/^.*Desktop//' -e 's/files.txt:.*$//'. This is a fairly trivial solution, and I'm sure there are better ones.

Comments

0

Id resort to awk:

BEGIN { FS="/" }

{
  for(i=1;i<NF;i++)
    if (length($i) == 1)
      a[NR]=a[NR]""$i
}
END {
  for (i in a)
    print a[i]
}

use it like this:

$ awk -f script.awk input
bor
yunus

or if you have your data in a variable:

$ awk -f script.awk <<< $data

4 Comments

But what if the input line is /home/q/Desktop/b/o/r/files.txt:am.torrent?
well, yes that is a problem since you'd get qbor but that format wasn't given in OPs tetstdata. I'll wait with a complete general solution until OP request such...
But the possibility was implied. /home/ke/Desktop suggests that he's scanning user homedirs. It isn't usual, but there's nothing forbidding a single-character username.
if it's the users home-dir thats an issue then simply change the for-loop to for(i=4;i<NF;i++) instead. Until OP states differently I'll stick to KISS.
0

it's not a nice/tidy solution, but bash parameter expansion is a powerful tool. So could not resist providing an example

[]l="/home/ke/Desktop/b/o/r/files.txt:am.torrent"
[]m=${l##*Desktop/}
[]n=${m%%/files.txt*}
[]k=${n//\//}
[]echo $m
b/o/r/files.txt:am.torrent
[]echo $n
b/o/r
[]echo $k
bor

You can see how nicely bash is replacing the variable step by step without using any external program (btw [] is PS1, prompt) There can be many more ways to do it. I got another one while writing the first

[]l="/home/ke/Desktop/b/o/r/files.txt:am.torrent"
[]m=${l/*Desktop\//}
[]n=${m/\/files.txt*/}
[]k=${n//\//}
[]echo $m
b/o/r/files.txt:am.torrent
[]echo $n
b/o/r
[]echo $k
bor

Try some more,

1 Comment

Sorry lost track of the original question, you can read line by line in a while to use this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.