LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   deleting particular lines using shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/deleting-particular-lines-using-shell-scripting-764748/)

ibabhelix 10-27-2009 05:33 AM

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

thegeek 10-27-2009 05:37 AM

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

ibabhelix 10-27-2009 05:41 AM

hey pls be elaborate i dint understand the solution sorry, could u pls tel me again ?
please

ibabhelix 10-27-2009 05:44 AM

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.

thegeek 10-27-2009 06:46 AM

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

ghostdog74 10-27-2009 06:51 AM

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

bsat 10-28-2009 12:07 AM

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

geek.ksa 10-28-2009 04:21 AM

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.

ibabhelix 11-02-2009 12:07 AM

Hey guys please help me i am not able to delete those lines using

cat testa.txt | grep "[0-9]\.[0-9]"

pixellany 11-02-2009 12:26 AM

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


All times are GMT -5. The time now is 05:42 PM.