Visit Our UNIX and Linux User Community


Deleting particular lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting particular lines.
# 1  
Old 10-09-2009
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

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

and output should be

Code:
cms/cms/images/services_icons

folder name can be any like

Code:
cms/cms/images/services_icon_version_2.1

in this case input can be

Code:
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

Code:
cms/cms/images/services_icon_version_2.1/


Thanks in advance.
# 2  
Old 10-09-2009
And a find . -type d can't do the job?
# 3  
Old 10-09-2009
Code:
$ 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

# 4  
Old 10-09-2009
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.

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

temp file is

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

key file is
Code:
"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

Code:
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

Code:
cp   cms/cms/images/services_icons/

deleting 2 more lines, which is wrong.

script using

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

# 5  
Old 10-09-2009
Assumed that all the files has 3 character extensions like .php, .png, .exe ,etc.

Try this,

Code:
$ 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 )
# 6  
Old 10-09-2009
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.
# 7  
Old 10-09-2009
Code:
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?

Previous Thread | Next Thread
Test Your Knowledge in Computers #158
Difficulty: Easy
The migration of the ARPANET to TCP/IP was officially completed on January 1, 1983, when the new protocols were permanently activated.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting all lines containing numbers

Hi guys I have a text file in the following format what i would like ot do is iterate through the file deleting the lines containing only numbers. I have googled this and have been unable to find any help ( maybe its my search terms) so if any one an give me a heads up i would... (3 Replies)
Discussion started by: dunryc
3 Replies

2. Shell Programming and Scripting

deleting lines in ex

Hello, im using ex to manipulate some text. Im trying to delete all the lines except those on which a certain regex can be found. the important part of the script: ex temp << 'HERE' g/regex/p HERE this command prints the lines I want to end up with, but it doesnt delete the others.... (2 Replies)
Discussion started by: drareeg
2 Replies

3. Shell Programming and Scripting

deleting lines from file

We have a server that logs transactions to a file. I want to write a script that will delete the first 50 lines of the file daily without renameing the file or moving the file. (8 Replies)
Discussion started by: daveisme
8 Replies

4. Shell Programming and Scripting

deleting last n lines from a output

Friends, I am executing this command in solaris sar -d 3 3 | awk 'NR > 2 { if ($1 !~ /,.+/) print }' | egrep -v "nfs|device" . Now i want to delete the last two lines of my output as they are records of average which i don't want. can some one pls give me some idea on how to proceed. (7 Replies)
Discussion started by: achak01
7 Replies

5. UNIX for Advanced & Expert Users

Deleting lines from a file

How I can delete 100 lines anywhere in a file without opening a file and without renaming the file. (11 Replies)
Discussion started by: Nirgude07
11 Replies

6. Shell Programming and Scripting

Deleting processed lines

I have a log file that I am processing. This contains messages from and to a server (requests and responses). The responses to requests may not be in order i.e. we can have a response to a request after several requests are sent, and in some error cases there may not be any response message. ... (2 Replies)
Discussion started by: BootComp
2 Replies

7. Shell Programming and Scripting

Deleting the similar lines

Dear Friends myself Avinash working in bash shell The problem goes like this I have a file called work.txt assume that first colum=mac address second colum= IP third colum = port number ---------------------------------------- 00:12:23:34 192.168.50.1 2 00:12:23:35 192.168.50.1 5... (2 Replies)
Discussion started by: avi.skynet
2 Replies

8. Shell Programming and Scripting

Deleting lines in a file

How do I delete all the lines after the line containing text ***DISCLOSURES*** . I want to delete this line too. Thank you (2 Replies)
Discussion started by: reachsamir
2 Replies

9. Shell Programming and Scripting

deleting lines

I am trying deleting lines from a text file using sed.. sed '/OBJECT="ABC/{N;N;N;d; }' will do if i have to delete lines starting with Object and next 3 lines but I was looking for a way to delet lines starting with OBJECT and all the lines till it reaches a blank lines ..or it reaches a... (8 Replies)
Discussion started by: ajnabi
8 Replies

10. Programming

deleting lines

I am spooling a file from oracle and trying to delete the last line of the spooled file which I am unable to do. Problem is that this file can have multiple records each time and I have no way of knowing how many because the amount can vary. I had an idea of using a while loop to read the... (1 Reply)
Discussion started by: supercbw
1 Replies

Featured Tech Videos