Operating System - HP-UX
1827757 Members
2924 Online
109969 Solutions
New Discussion

Re: delete lines and adding blank line

 
SOLVED
Go to solution
sheevm
Regular Advisor

delete lines and adding blank line

I have a file which has multiple occurences of the same line. I need to keep the first Occurance and delete the rest. Next thing I want to do is And also based on the value of some lines I need to insert a blank line.

Anyone out there can give me a hint?

Thanks
be good and do good
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: delete lines and adding blank line

Hi:

See 'uniq' for eliminating multiple occurances of a line.

For adding a blank line after a particular pattern, you could do:

# awk '{if ($0~/JKL/) {print $0"\n"} else {print $0}}' /tmp/input > /tmp/output

Regards!

...JRF...
Wodisch
Honored Contributor

Re: delete lines and adding blank line

Hi Raji,

how about using "awk" (even though it's an old tool, and Clay and procura will present a solution in "perl" ;-)?

awk -F script.awk your.file > new.file

and the script "script.awk":

#!/usr/bin/awk
BEGIN { count=0 }
"$0" == "your case#1" { count++; if (count <= 1) print }
"$0" == "your case#2" { print; print ""; }
# end of script.awk

HTH,
Wodisch
Helen French
Honored Contributor

Re: delete lines and adding blank line

Hi Raji:

Another simple way to delete multiple occurences of lines:

# sort -u filename > new_file

HTH,
Shiju
Life is a promise, fulfill it!
MANOJ SRIVASTAVA
Honored Contributor

Re: delete lines and adding blank line

Raji


try doing a sort and then counting the no. of lines for that acuurances and then deleteing the count -1 for that acouurance from that file. for eg
say a file has

aa
aa
aa
aa
bb
cc
cd
dd


and what o/p you want is
aa



bb
cc
cd
dd

you can try this:

#!/usr/bin/ksh

A=`cat | sort | grep aa |wc -l`
B=`expr $A - 1`

cat | sort > filename2
cat filename2 | sed -e "2,$B s/aa/ /g" > filename3


and filename3 is what you want .


Manoj Srivastava


sheevm
Regular Advisor

Re: delete lines and adding blank line

Thanks guys. I am trying to combine all the ideas.

It should work fine.
be good and do good