LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2008, 03:40 PM   #1
fs11
Member
 
Registered: Aug 2006
Posts: 79

Rep: Reputation: 15
First line and replace


Hello All,

I have a small problem, i would like to solve with bash scripting.(sed grep anything..).

I have a file and i want to search for the start of the file..if the pattern is not present and then to delete those lines..

forexample:

---------
>SEQ216
------------------------------------------------------------
------------------------------------------------------------
--------------------GLIHGGT--KAAPLNEGA----------------------
------------------------------------------------------------
---------------------------------------------------YELNQCASI
---------
>SEQ250
------------------------------------------------------------
------------------------------------------------------------
--------------------GTIYGFN--VMGVVCLYG----------------------
------------------------------------------------------------
---------------------------------------------------FEHDLCDAV
---------
>SEQ251
------------------------------------------------------------
------------------------------------------------------------
--------------------GVMKGAN--TMGVVCLYG----------------------
------------------------------------------------------------
---------------------------------------------------FKHDLCDAI
---------
>SEQ252
------------------------------------------------------------
------------------------------------------------------------
--------------------GVVSGGH--TMGVVCLYG----------------------
------------------------------------------------------------
---------------------------------------------------YEIERCDEI
---------

$$$$$$$$$$$$$$$$$$$$$

I would like that the first element in the file is a '>'..If this is not the case , then delete this line....

Any help would be highly appreciable.

Thanks in advance..
 
Old 04-24-2008, 05:46 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
sed '1{/^>/!d}' file >newfile
or
sed -i '1{/^>/!d}' file
The "1" selects the first line. The "/^>/!" test passes if the line doesn't start with "<". If that is the case the line is deleted.
 
Old 04-24-2008, 06:04 PM   #3
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
The code doesn't seem to work.
I am still getting all the lines in the file.
Please help !
 
Old 04-25-2008, 12:17 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Maybe I misunderstood what you want to do. From your description, you wanted to delete line 1 if it doesn't begin with at "<" character.
Code:
sed '1{/^>/!d}' testfile >newfile
jschiwal@hpmedia:~> head newfile
>SEQ216
------------------------------------------------------------
------------------------------------------------------------
--------------------GLIHGGT--KAAPLNEGA----------------------
------------------------------------------------------------
---------------------------------------------------YELNQCASI
---------
>SEQ250
------------------------------------------------------------
------------------------------------------------------------
Also, you probably should have put your sample in [ code ] blocks to preserve formatting. I think that as posted, the lines are split up so I couldn't test it on a read sample. The sample you posted has 32 lines, and the first was deleted because it didn't start with a "<".

If you want to delete all lines until one starts with a "<", then:
sed '1,/^>/{/^>/!d}' testfile

Last edited by jschiwal; 04-25-2008 at 12:33 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kate: Replace with a new line [KIA]aze Linux - Newbie 5 02-04-2021 05:39 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
C ++ I/O: Replace a line FreeDoughnut Programming 7 10-31-2006 08:21 PM
Replace line in file with two lines?! Possible? eur0dad Linux - General 2 09-07-2006 02:15 PM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration