Deleting particular lines.

hi all,

i have got a scenario in which i need to delete all the lines that ends with file names.

e.g.
input can be

cms/images/services_icons/callback.png 
cms/cms/images/services_icons/sync.php
cms/cms/images/services_icons

and output should be

cms/cms/images/services_icons

folder name can be any like

cms/cms/images/services_icon_version_2.1

in this case input can be

cms/images/services_icon_version_2.1/callback.png 
cms/cms/images/services_icon_version_2.1/sync.png 
cms/cms/images/services_icon_version_2.1

and output should be

cms/cms/images/services_icon_version_2.1/

Thanks in advance.

And a find . -type d can't do the job?

$ cat a
cms/images/services_icon_version_2.1/callback.png
cms/cms/images/services_icon_version_2.1/sync.png
cms/cms/images/services_icon_version_2.1

$ sed  '/\/[^\/]*\..../d' a
cms/cms/images/services_icon_version_2.1

ripat, Please complete syntax.

let me tell you what i am doing right now.

at the moment i have in put in a file named temp. and i check from another file name key ( this file has all extensions ). and then delete those lines.

for ext in `cat key`; 
do sed -i '/[.]'$ext'$/d' temp ;
done

temp file is

cp   cms/images/services_icons/callback.png 
cp   cms/cms/images/services_icons/sync.png 
cp   cms/cms/images/services_icons/

key file is

"php pdf png"

but issue is when there is new extension then i have to add it in key file otherwise it will not delete that line and creates problem.

so i just want to avoid from this key type thing.

---------- Post updated at 07:14 AM ---------- Previous update was at 06:59 AM ----------

sorry skmdu , its not working properly. when i change folder name then it fails

input

cp   cms/images/services_icons/callback.png 
cp   cms/cms/images/services_icons/sync.png 
cp   cms/cms/images/services_icons/
cp   cms/cms/images/icons.version_2/
cp   cms/cms/images/services_icons.cc_folder

and script gives me output

cp   cms/cms/images/services_icons/

deleting 2 more lines, which is wrong.

script using

sed  '/\/[^\/]*\..../d' 

Assumed that all the files has 3 character extensions like .php, .png, .exe ,etc.

Try this,

$ sed  -e '/\/[^\/]\+\....$/d' a

---------- Post updated at 07:26 AM ---------- Previous update was at 07:23 AM ----------

This solution wont work if your folder name is like
cp cms/cms/images/services_icons.cce ( 3 characters after dot )

skmdu sorry you were wrong, coz extension can be "mpeg".

i think its not possible to check either its a file or folder without having a list of extensions. so the way ,which i am currently doing, is right, although it creates problems.

but if you have any solution then please let me know. Thanks.

cp   cms/images/services_icons/callback.png 
cp   cms/cms/images/services_icons/sync.png 
cp   cms/cms/images/services_icons/

Does all these paths ( folders and files ) exist in a file system?

Your input file looks like the o/p of an ls command, so it was just a hint that the find command can display only the directories with the -type option.

find /path_to_scan/ -type d

sorry skmdu for the late reply.

yes all these paths exists in a file and i have to read that file and then i have to delete all those lines from that file which represent to files and keep only those lines which represent to folders.

and let me tell you one more thing that these paths are real path and exit on the server.

$cat b.sh

#! /usr/bin/sh

exec<"a" #a is a filename in which list of paths and filenames are there.
while read line
do
if [ -d $line ]
then
echo $line;
fi
done

$ mkdir /tmp/testing_this.txt
$ mkdir /tmp/testing_this.t.t

$cat a
/var/log
/tmp/testing_this.txt  
/tmp/testing_this.t.t
/home/rs/5389/autokick.diff
/home/rs/5389/
/home/rs/

$sh b.sh
/var/log
/tmp/testing_this.txt
/tmp/testing_this.t.t
/home/rs/5389/
/home/rs/

Thank You skmdu. its working fine. thanks again.

hi

sorry to disturb again. the solution is ok but i have got a problem with it.

exec<"tempFile" 
while read line
do
	if [ -d $line ]
	then
	echo $line ;
	fi
done

this script is ok when i run it alone. but when i use this thing in my other script then i get strange behavior. after completing task it doesn't quit and goes in some loop and i have to press crtl+c to end it.

so is there any other solution for it?

Try with this,

FILENAME="tempfile"
while read line
do
if [ -d $line ]
then
echo $line ;
fi
done <$FILENAME
Still if you find issue, post your whole script, if possible.

---------- Post updated at 01:54 AM ---------- Previous update was at 01:54 AM ----------

Try with this,

FILENAME="tempfile"
while read line
do
	if [ -d $line ]
	then
	echo $line ;
	fi
done <$FILENAME

Still if you find issue, post your whole script, if possible.

ok thanks skmdu. its working fine now. actually i noticed one thing that after exec my "read -ep" command wasn't working properly and was unable to prompt for any input. may be there is some other way to use it. anyhow its working now. thanks.