Delete specific lines from files based on another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete specific lines from files based on another file
# 1  
Old 01-21-2014
Delete specific lines from files based on another file

I have some text files in a folder named ff as follows. I need to delete the lines (in-place editing)in these files based on another file aa.txt.

32bm.txt:
Code:
249  253 A P        -     0   0    8      0, 0.0     6,-1.4     0, 0.0     2,-0.4  -0.287  25.6-102.0 -74.4 161.1   37.1   13.3   10.9
250  254 A K  B      Z  254   0E  77    -48,-2.5   -48,-0.3     4,-0.2     4,-0.3  -0.720 360.0 360.0 -93.4 135.2   38.1   11.1    8.1
252        !*             0   0    0      0, 0.0     0, 0.0     0, 0.0     0, 0.0   0.000 360.0 360.0 360.0 360.0    0.0    0.0    0.0
253  143 B R              0   0   96      0, 0.0    -2,-3.7     0, 0.0     2,-0.2   0.000 360.0 360.0 360.0 110.4   38.4   10.4    3.0
254  144 B Q  B     -Z  250   0E  62     -4,-0.3    -4,-0.2    -3,-0.1     2,-0.1  -0.347 360.0-157.5 -58.1 119.5   39.4   13.6    4.8
255  145 B T        -     0   0   22     -6,-1.4     2,-0.3    -2,-0.2    -7,-0.2  -0.396   7.8-127.4 -91.5 173.9   36.3   15.7    5.4

2fok.txt:
Code:
1  361 X G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
2  362 X A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
3  363 X R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0
1  361 B G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
2  362 B A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
3  363 B R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0

aa.txt
Code:
32bm    B   143 145
2fok    X   361 363
2moj    B   361 367
-
-
-

For example, in the 32bm.txt, I need only the lines having B (column3) and the numbers from 143 to 145 (column2).

Desired output:

32bm.txt
Code:
253  143 B R              0   0   96      0, 0.0    -2,-3.7     0, 0.0     2,-0.2   0.000 360.0 360.0 360.0 110.4   38.4   10.4    3.0
254  144 B Q  B     -Z  250   0E  62     -4,-0.3    -4,-0.2    -3,-0.1     2,-0.1  -0.347 360.0-157.5 -58.1 119.5   39.4   13.6    4.8
255  145 B T        -     0   0   22     -6,-1.4     2,-0.3    -2,-0.2    -7,-0.2  -0.396   7.8-127.4 -91.5 173.9   36.3   15.7    5.4

2fok.txt
Code:
1  361 X G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
2  362 X A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
3  363 X R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0

your suggestions would be greatly appreciated!
# 2  
Old 01-21-2014
Here is one way of doing this:
Code:
#!/bin/bash

for file in *.txt
do
        [ "$file" = "aa.txt" ] && continue

        echo "Fixing file: $file"

        awk '
                NR == FNR {
                        A[$1] = $0
                        next
                }
                !(F) {
                        F = FILENAME
                        sub ( /\..*/, X, F )
                }
                F in A {
                        split ( A[F], R )
                        if ( $3 == R[2] && $2 >= R[3] && $2 <= R[4] )
                                print $0
                }
        ' aa.txt "$file" > tmp

        mv tmp "${file}.new"
done

Note that I am creating output file with .new as suffix. You may remove it if the output looks good.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-21-2014
Try
Code:
awk '{FN=$1".txt"; KR=$2; LL=$3; LH=$4; while (getline < FN) if ($3==KR && $2 >= LL && $2 <= LH) print}' aa.txt

You may need to print to individual files, e.g. FN".new".
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific lines based on another file

I have a folder containing text files. I need to extract specific lines from the files of this folder based on another file input.txt. How can I do this with awk/sed? file1 ARG 81.9 8 81.9 0 LEU 27.1 9 27.1 0 PHE .0 10 .0 0 ASP 59.8 11 59.8 0 ASN 27.6 12 27.6 0 ALA .0 13 .0 0... (5 Replies)
Discussion started by: alanmathew84
5 Replies

2. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

3. UNIX for Dummies Questions & Answers

How to delete specific lines (2n+3 line, n=0,1,2...296) in a file?

Dear everyone, I have a file with 900 lines (there is only numbers in one line, no string), I only need the lines 2+3n (n=0,1...296), i.e line 2, 5, 8, 11...888. I tried google but only the results such as how to delete all the odd lines or all the even lines with 'awk' command. Thanks in... (4 Replies)
Discussion started by: phamnu
4 Replies

4. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

5. Shell Programming and Scripting

Delete files based on specific MMDDYYYY pattern in filename

Hi Unix gurus, I am trying to remove the filenames based on MMDDYYYY in the physical name as such so that the directory always has the recent 3 files based on MMDDYYYY. "HHMM" is just dummy in this case. You wont have two files with different HHMM on the same day. For example in a... (4 Replies)
Discussion started by: shankar1dada
4 Replies

6. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

7. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

8. Shell Programming and Scripting

Delete specific lines from a file

Hi, I have a file ( all_users.ldif ) of the following format: cn=orcladmin, cn=Users, dc=maximus,dc=com cn=PUBLIC, cn=Users, dc=maximus,dc=com cn=portal,cn=users,dc=maximus,dc=com cn=portal_admin,cn=users,dc=maximus,dc=com cn=uddi_publisher,cn=Users,dc=maximus,dc=com... (4 Replies)
Discussion started by: itzz.me
4 Replies

9. Shell Programming and Scripting

Delete lines prior to a specific date in a log file.

Hi all. I have a database log file in which log data get appended to it daily. I want to do a automatic maintainence of this log by going through the log and deleting lines belonging to a certain date. How should i do it? Please help. Thanks. Example. To delete all lines prior to Jun... (4 Replies)
Discussion started by: ahSher
4 Replies

10. Programming

Delete specific lines in a text file

Hi, experts, I would like to create a function that can calculate the total number of lines in a saved text file and delete specific lines in that particular file (I only want the last few lines). Hav anybody have the experience and giv me a hand in this? (9 Replies)
Discussion started by: dniz
9 Replies
Login or Register to Ask a Question