LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2009, 06:33 AM   #1
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Rep: Reputation: 18
deleting particular lines using shell scripting


i ve written a program which gives the output something like this,:

Code:
C4'   A A  25	P     C A  26	3.909728 
P     C A  26	C4'   C A  26	3.963673 
P     C A  26	P     U A  27	6.040643 
C4'   C A  26	P     U A  27	3.892993 
P     U A  27	C4'   U A  27	3.962986 
P     U A  27	P     C A  28	6.240974 
C4'   U A  27	P     C A  28	3.907594 
P     C A  28	C4'   C A  28	3.962415 
P     C A  28	P     C A  29	5.776231 
C4'   C A  28	P     C A  29	
P     C A  29	C4'   C A  29	
P     C A  29		
C4'   C A  29
now here i wanna delete unwanted lines which i have colored in red, how do i do tat ?
using sed command we can delete lines but in this case what should i do/

please help me
 
Old 10-27-2009, 06:37 AM   #2
thegeek
Member
 
Registered: Oct 2009
Location: Amsterdam
Distribution: CentOS,Fedora,Puppy
Posts: 62

Rep: Reputation: 20
Assuming the file /tmp/disco contains that output:

[dancing@jonasandneil tmp]$ cat /tmp/disco | head -9
C4' A A 25 P C A 26 3.909728
P C A 26 C4' C A 26 3.963673
P C A 26 P U A 27 6.040643
C4' C A 26 P U A 27 3.892993
P U A 27 C4' U A 27 3.962986
P U A 27 P C A 28 6.240974
C4' U A 27 P C A 28 3.907594
P C A 28 C4' C A 28 3.962415
P C A 28 P C A 29 5.776231
 
Old 10-27-2009, 06:41 AM   #3
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
hey pls be elaborate i dint understand the solution sorry, could u pls tel me again ?
please
 
Old 10-27-2009, 06:44 AM   #4
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
oh u r cutting the lines after 9 lines, but my code gives files with different number of lines , so how do i do then ?
i mean i dono how many number of lines r there, i just know the pattern of lines.
 
Old 10-27-2009, 07:46 AM   #5
thegeek
Member
 
Registered: Oct 2009
Location: Amsterdam
Distribution: CentOS,Fedora,Puppy
Posts: 62

Rep: Reputation: 20
This one uses a regular expression to ensure the line contains a float with at least number.number

omg:~# cat testa.txt | grep "[0-9]\.[0-9]"
C4' A A 25 P C A 26 3.909728
P C A 26 C4' C A 26 3.963673
P C A 26 P U A 27 6.040643
C4' C A 26 P U A 27 3.892993
P U A 27 C4' U A 27 3.962986
P U A 27 P C A 28 6.240974
C4' U A 27 P C A 28 3.907594
P C A 28 C4' C A 28 3.962415
P C A 28 P C A 29 5.776231
 
Old 10-27-2009, 07:51 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
describe your criteria for deletion clearly next time. From what i can see, you just don't want lines that are less than 9 fields.
Code:
awk 'NR<9' file
 
Old 10-28-2009, 01:07 AM   #7
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
I am assuming you want to delete the lines that do not have the last column in the output.
The following script will work if the format of the rest of the columns remain the same,
put the script into a file say "delete_lines"

{
if(NF<=8)
print $0
}

Assuming the data is stored in file called test_file
and run the script using the command

awk -f delete_lines test_file
 
Old 10-28-2009, 05:21 AM   #8
geek.ksa
Member
 
Registered: Jan 2009
Location: Dhahran, Saudi Arabia
Distribution: RHEL 5
Posts: 42

Rep: Reputation: 17
assuming that your program output is in the file "file". However you better pipe the output of your program to below awk; it's neater, e.g. ( #myprog | below awk )

Code:
naif@master ~/LQ> cat file
C4'   A A  25   P     C A  26   3.909728
P     C A  26   C4'   C A  26   3.963673
P     C A  26   P     U A  27   6.040643
C4'   C A  26   P     U A  27   3.892993
P     U A  27   C4'   U A  27   3.962986
P     U A  27   P     C A  28   6.240974
C4'   U A  27   P     C A  28   3.907594
P     C A  28   C4'   C A  28   3.962415
P     C A  28   P     C A  29   5.776231
C4'   C A  28   P     C A  29
P     C A  29   C4'   C A  29
P     C A  29
C4'   C A  29
naif@master ~/LQ> nawk '{ if($9 != "") print $0 }' file
C4'   A A  25   P     C A  26   3.909728
P     C A  26   C4'   C A  26   3.963673
P     C A  26   P     U A  27   6.040643
C4'   C A  26   P     U A  27   3.892993
P     U A  27   C4'   U A  27   3.962986
P     U A  27   P     C A  28   6.240974
C4'   U A  27   P     C A  28   3.907594
P     C A  28   C4'   C A  28   3.962415
P     C A  28   P     C A  29   5.776231

You can ofcourse redirect the awk output to some other file. o

btw, i used nawk, awk should work as well.
 
Old 11-02-2009, 01:07 AM   #9
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
Hey guys please help me i am not able to delete those lines using

cat testa.txt | grep "[0-9]\.[0-9]"
 
Old 11-02-2009, 01:26 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You don't say if you tried any of the other solutions----or what happened when you tried the grep. (It worked for me)

Here is another method (same Regex as the grep):
Code:
sed '/[0-9]\.[0-9]/!d' filename
Note that you typically do not need to use "cat". Also, for the deletion to be permanent, you need to write to a new file---eg:

Code:
sed '/[0-9]\.[0-9]/!d' filename > newfilename
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
variable stored with times on seperate lines i need to add them up shell scripting xskycamefalling Programming 4 05-16-2009 03:11 AM
Deleting the lines from a file using shell scripts sharad Linux - General 1 05-22-2006 04:17 AM
Inserting lines into a file through shell scripting false-hopes Linux - General 1 10-22-2005 12:39 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM


All times are GMT -5. The time now is 09:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration