Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove lines 3

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
with slash (or multiple slashed) on the beginning of lines but also such slash(es) led and/or trailed by spaces/tabs and also lines with space in a direcotry name (usually typo)

Example of lines which should be rid off:

Code:
/
 /  dgf / dfg
   ////
///////
/sfsd/sfd /
/sfsd/sfdsfs /dfgf/df/ 
/dfgdg /gddd /

and which should NOT be removed
Code:
///sfsffs
/d/d/d/d
  /ssfsfsf

Multiple occurences of / (like in example ///sfsffs) could also be replaced by single / (/sfsffs)

I want to avoid "/" filesystem "representants" from the file.


 
Code:
# touch ^Uadfasd  (created using ctrl-v ctrl-u)
# ls                                                    
adfasd
# rm adfasd
rm: adfasd: A file or directory in the path name does not exist.

# set -A arr $(ls .)
# echo ${arr[*]}
adfasd
# rm ${arr[*]}
# ls
#
 


ok, I think this will do the job:

Code:
sed "s/^[[:space:]]*//;s/[[:space:]]*$//" test_file|sed s/[[:space:]]*//g|sed s,///*,/,g|grep ^\/|grep -v ^\/$
 
Code:
sed 's#//*#/#g' inputfile | awk '{for (i=1;i<=NF;i++) { if ($i == "/") next } ; print }'

The sed compresses multiple slashes into one. The awk part rejects lines with an isolated "/".

Note that I have allowed "/dfgf/df/" because it is perfectly acceptable to terminate a directory name with a slash; in fact in many cases preferable because it explicitly specifies a directory rather than a file.



Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Something like this ?
Code:
awk 'NF==1&&$1!~"^/*$"' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, that wouldn't remove these two lines:

Code:
/sfsd/sfd /
/sfsd/sfdsfs /dfgf/df/
/dfgdg /gddd /

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
PHV, that wouldn't remove these two lines
Really ?
awk '[!]NF==1[/!]&&$1!~"^/*$"' /path/to/input
 
True, true... okay, but it *would* remove a line with two valid directories, e.g. "/somepath /some/other/path".

I assume that is not what the OP wanted.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
I assume that is not what the OP wanted
Reread the 1st post ...
 
w5000 said:
also lines with space in a direcotry name (usually typo)

Hmm... okay... I'll crawl back under my stone now. :)

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top