Delete specific parts in a .txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete specific parts in a .txt file
# 1  
Old 02-27-2013
Blade Delete specific parts in a .txt file

Hi all,

I desperately need a small script which deletes everything in a particular .txt file when "Abs = {" appears till "},", and also when "B-1 = {" appears till "},"

I would like all the text in between of those instances to be deleted, however, other text to be unedited (kept as it is).

Could anyone help? Smilie

Thank you!

Last edited by c_lady; 02-27-2013 at 01:09 PM..
# 2  
Old 02-27-2013
I think it will be better if you can post sample data and desired output in code tags.
# 3  
Old 02-27-2013
Hi,
I just joined today to ask a question in another forum (AIX), so I thought I'd give something back to unix.com to help out.

Here's a started, with the limited detail's you provided.

text.txt:
Code:
line1
line2
Abs = {start marker}
line3
line4
B-1 = {end marker}
line5
line6

read.pl
Code:
#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;

print_inside(); 
print "\n";
print_outside(); 
 
sub print_inside {
     
    my $filename = 'text.txt';
    open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
    my $output = 0;
    
    while (<$fh>) {
        chomp;
        # print "$_ \n";
        
        if ( /Abs.*=.*{(.*)}/i ) {
            print "===S===> " . $1 . "\n";
            $output = 1;
        } elsif ( /B-1.*=.*{(.*)}/i ) {
            print "===E===> " . $1 . "\n";
            $output = 0;
        } elsif ($output) {
            print $_ . "\n"   
        }
        
    }
}

sub print_outside {

    my $filename = 'text.txt';
    open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
    my $output = 1;
     
    while (<$fh>) {
        chomp;
        # print "$_ \n";
        
        if ( /Abs.*=.*{(.*)}/i ) {
            $output = 0;
        } elsif ( /B-1.*=.*{(.*)}/i ) {
            $output = 1;
        } elsif ($output) {
            print $_ . "\n"   
        }
        
    }
}

output:
Code:
 $ ./read.pl
===S===> start marker
line3
line4
===E===> end marker

line1
line2
line5
line6


Last edited by aix_user1; 02-27-2013 at 01:39 PM.. Reason: typo
# 4  
Old 02-27-2013
Data:

Code:
@article{Test1:2011aa,
    Abs = {Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.},
    Author = {Lorem Ipsum and Ipsum Lorem},
    Date-Added = {2013-02-27 2:55:51 +0200},
    Title = {Geometric analogue of holographic reduced representation},
    Year = {2009},
    B-1 = {http://www.google.com}}

@book{Test2:2012bb,
    Abs = {Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.},
Author = {Lorem Ipsum and Ipsum Lorem},
    Date-Added = {2012-02-27 3:44:41 +0400},
    Year = {2010},
    B-1 = {http://www.google.com}}

Desired output:

Code:
@article{Test1:2011aa,
    Author = {Lorem Ipsum and Ipsum Lorem},
    Date-Added = {2013-02-27 2:55:51 +0200},
    Title = {Geometric analogue of holographic reduced representation},
    Year = {2009}}

@book{Test2:2012bb,
    Author = {Lorem Ipsum and Ipsum Lorem},
    Date-Added = {2012-02-27 3:44:41 +0400},
    Year = {2010}}


Last edited by vgersh99; 02-27-2013 at 05:09 PM.. Reason: edited code tags
# 5  
Old 02-27-2013
Code:
awk '!/^[ \t]+Abs/ && !/^[ \t]+B-1/' filename

# 6  
Old 02-27-2013
Thanks! But one more thing... after deleting:

B-1 = {http://www.google.com}}

I would like the last brackets to be taken to the previous row, and the comma in the previous row to be deleted... so that the last line it ends up like this.

Year = {2010}}

Notice that, the last line can be "Year" or anything else, but always the B-1 comes to the end, and I would like the above thing to happen (the last brackets to be taken to the previous row, and the comma in the previous row to be deleted).

Thanks!
# 7  
Old 02-27-2013
Code:
awk ' $0 ~ /^[ \t]+Abs.*/  {
                next
} $0 !~ /^[ \t]+B-1.*/ {
                p = (p=="")?RS $0:p RS $0
} $0 ~ /^[ \t]+B-1.*/ {
                sub(/,$/,"}",p)
                print p
                p = ""
} ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Delete files in a txt file

Hi, I have very old files in my server like from 2012 and i want to delete them, Please help. Thanks in advance.. (2 Replies)
Discussion started by: nanz143
2 Replies

2. UNIX for Dummies Questions & Answers

Delete .txt file from current directory

I have created few text file during run time by redirecting the txt file echo USER_LIST_"$(date '+%d%h%Y')".csv > report_location.txt report_location.txt is creating in the same location where I run script home/script When I try to remove the txt file at the end of the... (3 Replies)
Discussion started by: stew
3 Replies

3. UNIX for Dummies Questions & Answers

Delete files whose file names are listed in a .txt file

hi, I need a help. I used this command to list all the log files which are for more than 10 days to a text file. find /usr/script_test -type f -mtime +10>>/usr/ftprm.txt I want all these files listed in the ftprm.txt to be ftp in another machine and then rm the files. Anyone can help me... (8 Replies)
Discussion started by: kamaldev
8 Replies

4. Shell Programming and Scripting

Delete file2.txt from file1.txt using scripting

Hi, I`m a total newbie, well my requirement is that i have 2 files I want to identify which countries i do not currently have in db.. how can i use the grep or another command to find this file .. i want to match all-countries.txt with countries-in-db.txt so the output is equal to... (11 Replies)
Discussion started by: beanbaby
11 Replies

5. UNIX for Dummies Questions & Answers

recursive delete files from txt file or?

i have a txt file of image names that have to be deleted in pwd how can i use the txt file to delete the files in pwd and is it possible?--i might be able to import the txt files into a spreadsheet ahd same it as a csv file. i want it to be done recursive lly --what i mean is teh sysem goes thru... (4 Replies)
Discussion started by: plener
4 Replies

6. Shell Programming and Scripting

Script to delete all something.txt~ file from a directory

There are some files in a directory like a.tx~ , b.txt~,c.txt~. I want to delete all these files inside that directory and sub directory. How can i do this? #!/bin/bash cd thatdirectory ...... rm -rf *~ ...... (7 Replies)
Discussion started by: cola
7 Replies

7. UNIX for Dummies Questions & Answers

Help with copying specific parts of a file structure

Hello. I need help with copying part of a file structure to another directory while still keeping the structure. For example look below: ../folder1/sub1/txt.txt ../folder1/sub2/pic.png ../folder2/sub1/pic.png ../folder2/sub2/txt.txt So in this I would like to copy only the directories and... (3 Replies)
Discussion started by: the
3 Replies

8. Shell Programming and Scripting

How to copy specific file.txt in specific folder?

hye there... i have a problem to copy file in specific folder that will change the name according to host,time(%m%s) and date(%Y%M%D) example folder name: host_20100531.154101801 this folder name will always change... but i just want to copy the AAA.txt and BBB.txt file.. really need... (17 Replies)
Discussion started by: annetote
17 Replies

9. Shell Programming and Scripting

How can i break a text file into parts that occur between a specific pattern

How can i break a text file into parts that occur between a specific pattern? I have text file having various xml many tags like which starts with the tag "<?xml version="1.0" encoding="utf-8"?>" . I have to break the whole file into several xmls by looking for the above pattern. All the... (9 Replies)
Discussion started by: abhinav192
9 Replies

10. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies
Login or Register to Ask a Question