Operating System - HP-UX
1837193 Members
2683 Online
110114 Solutions
New Discussion

Removing line from text file

 
SOLVED
Go to solution
Sachin Patel
Honored Contributor

Removing line from text file

Hi All,
I have a text file tail of that is like.

zeltweg IN CNAME zeltweg.abc.co.uk.
zeltweg IN CNAME zeltweg.xyz.co.uk.
zolder IN CNAME zolder.abc.co.uk.
zolder IN CNAME zolder.xyz.co.uk.
zonta IN CNAME zonta.abc.co.uk.
zonta IN CNAME zonta.xyz.co.uk.
zuela7 IN CNAME zuela7.abc.com.
zorzi IN CNAME zorzi.abc.co.uk.
zorzi IN CNAME zorzi.xyz.co.uk.

I would like to remove these lines from bourne shell script.

zeltweg IN CNAME zeltweg.xyz.co.uk.
zolder IN CNAME zolder.xyz.co.uk.
zonta IN CNAME zonta.xyz.co.uk.
zorzi IN CNAME zorzi.xyz.co.uk.

Sachin

Is photography a hobby or another way to spend $
9 REPLIES 9
Darrell Allen
Honored Contributor

Re: Removing line from text file

Hi Sachin,

If you want to delete all lines ending with "xyz.co.uk." use:

grep -v "xyz.co.uk.$" file >newfile

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
MANOJ SRIVASTAVA
Honored Contributor

Re: Removing line from text file

Hi Sachin

cat filename | grep -v xyz > new filename


Manoj Srivastava
Jeff Schussele
Honored Contributor

Re: Removing line from text file

Hi Sachin,

Use
grep -v "xyz.co.uk" filename > newfilename;mv filename filename.old;mv newfilename filename

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor

Re: Removing line from text file

If it were me, I would use sed. First create a file with the delete instructions, called myfile.sed
e.g

/zeltweg IN CNAME zeltweg.xyz.co.uk./d
/zolder IN CNAME zolder.xyz.co.uk./d
/zonta IN CNAME zonta.xyz.co.uk./d
/zorzi IN CNAME zorzi.xyz.co.uk./d

Bracket each desired pattern with /pattern/ and then end each line with 'd' - the delete command.

Now
sed -f myfile.sed oldfile > newfile
If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: Removing line from text file

Another variation here ..

# grep -v -E '\.xyz.' filename > newfile
==> filter out any lines that has ".xyz." string.
Sachin Patel
Honored Contributor

Re: Removing line from text file

Sorry guys I didn't explain very much
some time it is different. I had post tail of my file and so it come out xyz.co.uk but at the beginning it is different.

In sort when I had two line with same system name regarless of domain name I would like to remove second system name.

I guss i am still not explaining clearly.

Sachin
Is photography a hobby or another way to spend $
Sachin Patel
Honored Contributor

Re: Removing line from text file

Well I figure it out

last=null
for LINE in `cat file`
do
current = echo $LINE |awk '{print $1}'
if current != last
output line
fi
last=current
done

Sachin
Is photography a hobby or another way to spend $
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Removing line from text file

Okay, try this:

sort oldfile | sort -k1,3 -u > newfile

The use of two sorts is intentional, I want to sort first on the entire line to force exact order and then sort on the first 3 fields and only pass lines that are unique in the first 3 fields.
If it ain't broke, I can fix that.
Sachin Patel
Honored Contributor

Re: Removing line from text file

Hi Clay,
Your last answer works like champ. That is why you are Olympian and I am not....

Sachin
Is photography a hobby or another way to spend $