Operating System - HP-UX
1837981 Members
1866 Online
110124 Solutions
New Discussion

Re: deleting something from a file

 
SOLVED
Go to solution
M. Tariq Ayub
Regular Advisor

deleting something from a file

I have a file file01 contents

.
..
test
ile01
file02
file03

i want to delete the . and .. so that the content of the file looks like

test
ile01
file02
file03
9 REPLIES 9
Indira Aramandla
Honored Contributor

Re: deleting something from a file

Hi,

You can open the file using vi editior and delete the first 2 line and save it. then the file will have the required line.

or use sed or awk to print line from 3 till the end to another file.

eg:-
sed -n -e 3,6p fil01 > tempfile
This will take lines from 3 to 6 (if the number of line in the file are 6) and tempfile will have lines excluding . and ..


IA
Never give up, Keep Trying
Biswajit Tripathy
Honored Contributor
Solution

Re: deleting something from a file

Try:
# cat file01 | grep -v -e '^[ ]*\.[ ]*$' -e '^[ ]*\.\.[ ]*$' > file02

"file02" will have what you want.

Note that the above solution will handle
corner cases like:
1) when a file name itself has . or .. char
2) . and .. files names are not at the
beginning of line and might prepended or
appended with blank spaces.

Hope this helps.
- Biswajit
:-)
Muthukumar_5
Honored Contributor

Re: deleting something from a file

You can do it as,

grep -v '^\.' file01 > file02
mv file02 file01

or

perl -i -ne '$_=~s/^\.*//;print if !/^$/' file01

hth.
Easy to suggest when don't know about the problem!
Vibhor Kumar Agarwal
Esteemed Contributor

Re: deleting something from a file

This will also work

sed -e '/\.$/d' file_name > new_file

It will be a general case or a precise one will be deleting the 1st and 2nd lines

sed -e 1,2d file_name > new_file
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: deleting something from a file

Hi,

Solution of,
# cat file01 | grep -v -e '^[ ]*\.[ ]*$' -e '^[ ]*\.\.[ ]*$' > file02

is having few issues as,

1) cat file01 | is very slow. You can try < file01 for this as,

grep -v -e '^[ ]*\.[ ]*$' -e '^[ ]*\.\.[ ]*$' < file01 > file02

2) Why you are checking spaces after . or .. ? It may be very simple as,

grep -v '^[ ]*\.*[ ]*$' < file01 > file02

3) This operation will need temporary file. You can use perl -i to update into same file as,

perl -i -ne '$_=~s/^[ ]*\.*[ ]*$//;print if !/^$/'

hth.
Easy to suggest when don't know about the problem!
Biswajit Tripathy
Honored Contributor

Re: deleting something from a file

Muthukumar wrote:
> 2) Why you are checking spaces after . or ..
> ? It may be very simple as,
>
> grep -v '^[ ]*\.*[ ]*$' < file01 > file02

That would match '...' too, which would be
incorrect.

- Biswajit
:-)
Muthukumar_5
Honored Contributor

Re: deleting something from a file

Biswajit,

It will match for ... also. Check out as,

# echo '...' | grep '^[ ]*\.*[ ]*$'

Where,

^ - starting point
[ ]* - 0 or n spaces
\.* - . (dot) with zero or n times (if you want 1 or more times then use + instead of *)
[ ]* - 0 or n spaces
$ - end point

# echo "..." | grep -v '^[ ]*\.*[ ]*$'
#

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: deleting something from a file

sorry. For not matching ... and negate match of only . and .. then,

grep -Ev '^[ ]*\.[ ]*$|^[ ]*\.\.[ ]*$' > outputfile.

hth.
Easy to suggest when don't know about the problem!
Biswajit Tripathy
Honored Contributor

Re: deleting something from a file

Muthukumar,
Using '|' to separate two search strings (like
your previous reply) is essentially same
(performance wise) as using '-e' option twice
(as my first reply), AFAIK.

- Biswajit
:-)