LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed help (https://www.linuxquestions.org/questions/linux-newbie-8/sed-help-4175467227/)

mreff555 06-24-2013 05:50 PM

sed help
 
I have a <newline> separated list of files at various websites. eg.

Code:

http://blah.com/thing1.tar.gz
http://blah.com/thing2.tar.gz

I want to write a script to read this list and determine if all these files are in a directory.

I think I can figure out the rest, but I'm having trouble working out a sed statement to take a line of text and remove everything before and including the last forward slash. So I'm just left with a filename.

chrism01 06-24-2013 06:09 PM

How about param expansion
Code:

t='http://blah.com/thing1.tar.gz'

 echo ${t##*/}
thing1.tar.gz

http://wiki.bash-hackers.org/syntax/...string_removal

linuxCode 06-24-2013 06:27 PM

Try this script

Code:

#/bin/bash

while read line
do
if [ -f $(echo ${line##*/}) ]; then echo "${line##*/} exists"
else
echo "${line##*/} does not exists"
fi
done < list

Replace list with the file of http links

This script will extract the filename from the http link and check the current directory for existence of file or not

Bonus Code with Awk:

Code:

awk -F "/" '{print $NF }' list

mreff555 06-25-2013 09:41 AM

Thanks guys, it worked great.

linuxCode 06-25-2013 03:53 PM

You're welcome mreff555

Mark the thread solved from the threads tool if you feel it was solved.

David the H. 06-28-2013 05:06 AM

Quote:

Originally Posted by linuxCode (Post 4977868)
Code:

if [ -f $(echo ${line##*/}) ]; then echo "${line##*/} exists"

Useless Use Of Echo. Just evaluate the expansion directly.

Also, when using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

If you use single brackets you have to make sure to enclose the string in quotes so that it's properly protected from word-splitting and glob expansion.

And finally, the formatting could be a bit cleaner.

I'd personally write it more like this:

Code:

#/bin/bash

listfile=/path/to/list.txt

while read -r line || [[ -n $line ]]; do

    line=${line##*/}

    if [[ -f $line ]]; then
        echo "$line exists"
    else
        echo "$line does not exist"
    fi

done < "$listfile"

The extra [[ -n $line ]] test protects against the possibility of the input having no final newline.

linuxzilla.com 06-28-2013 05:45 AM

Quote:

Originally Posted by linuxCode (Post 4977868)
Try this script

Code:

#/bin/bash

while read line
do
if [ -f $(echo ${line##*/}) ]; then echo "${line##*/} exists"
else
echo "${line##*/} does not exists"
fi
done < list

Replace list with the file of http links

This script will extract the filename from the http link and check the current directory for existence of file or not

Bonus Code with Awk:

Code:

awk -F "/" '{print $NF }' list

Well that is great & I've saved it with me. Very smart script you've written. Thanks :)))

linuxCode 06-28-2013 08:05 PM

Quote:

Originally Posted by David the H. (Post 4980137)
Useless Use Of Echo. Just evaluate the expansion directly.

Also, when using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

If you use single brackets you have to make sure to enclose the string in quotes so that it's properly protected from word-splitting and glob expansion.

And finally, the formatting could be a bit cleaner.

I'd personally write it more like this:

Code:

#/bin/bash

listfile=/path/to/list.txt

while read -r line || [[ -n $line ]]; do

    line=${line##*/}

    if [[ -f $line ]]; then
        echo "$line exists"
    else
        echo "$line does not exist"
    fi

done < "$listfile"

The extra [[ -n $line ]] test protects against the possibility of the input having no final newline.

Thanks David the H. for the bash tips. :)

+1

linuxCode 06-28-2013 08:07 PM

Quote:

Originally Posted by linuxzilla.com (Post 4980161)
Well that is great & I've saved it with me. Very smart script you've written. Thanks :)))

You're Welcome :)


All times are GMT -5. The time now is 07:06 PM.