Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1954  / 2 Years ago, fri, august 5, 2022, 2:06:16

When I copy any file and paste it in console or text editos it is passed as




file:///home/user/path/file




when I pass it to script it is not found



What is the easiest way to convert that to normal linux path or somehow make script support it?



for example




cat file:///home/user/path/file




says




No such file or directory



More From » bash

 Answers
2

To remove the file:// prefix from the URL, you can use sed:



echo "file:///home/user/path/file" | sed "s/^file:////g"


What the above does:




  • Displays the URL to the standard output (so it can be modified with sed)

  • Replaces all occurrences of file:// in any line that begins with file:// with nothing. This effectively removes file:// from the URL leaving only /home/user/path/file



To use this from a script you can try the following:



cat $(echo "file:///home/user/path/file" | sed "s/^file:////g")


Now the error message is:



cat: /home/user/path/file: No such file or directory


(Please note that it refers to the correct filename instead of the URL.)



It would be much cleaner to store the converted filename in a shell variable and use it afterwards.



MYFILE=$(echo "file:///home/user/path/file" | sed "s/^file:////g")
cat $MYFILE

[#42356] Friday, August 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ampolinhad

Total Points: 88
Total Questions: 100
Total Answers: 116

Location: South Georgia
Member since Tue, Feb 1, 2022
2 Years ago
;