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
# 8  
Old 02-27-2013
give this a shot:

Code:
#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;

strip_file(); 
 
sub strip_file {
     
    my $filename = 'text.txt';
    open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";    
        
    my $last = <$fh>; # read ahead 1 line
    while (<$fh>) {
        chomp;
        my $cur = $_;
               
        if ( $last =~ /\@/ ) {             
            print $last;
        } elsif ( $last =~ /(.*).*=.*{(.*)}/i ) {
            my $key = $1;
            my $val = $2;
            
            if ( $cur =~ /B-1.*=.*{(.*)}?(.*)}/i ) {
                print "$key = {$val}}" . "\n\n";
                $cur = '';
            } elsif ( $cur ne '') {                
                print $last . "\n"                
            }            
        }
        $last = $cur;
    }
}

# 9  
Old 02-27-2013
I save the script in Desktop as script.awk and then try to execute it, but I get an error like:

awk -f script.awk
awk: syntax error at source line 1 source file script.awk
context is
awk >>> ' <<<
awk: bailing out at source line 1

This is what I have inside the script.awk

awk '!/^[ \t]+Abs/ && !/^[ \t]+B-1/' random.txt

Help?
# 10  
Old 02-27-2013
If you are using SunOS or Solaris, then use nawk instead of awk
Code:
nawk ' $0 ~ /^[ \t]+Abs.*/  {
                next
} $0 !~ /^[ \t]+B-1.*/ {
                p = (p=="")?RS $0:p RS $0
} $0 ~ /^[ \t]+B-1.*/ {
                sub(/,$/,"}",p)
                print p
                p = ""
} ' filename

OR
Code:
nawk '!/^[ \t]+Abs/ && !/^[ \t]+B-1/' filename

# 11  
Old 02-27-2013
I use Mac OS X, and if type nawk then -> -bash: nawk: command not found

if I type awk, then:

usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
# 12  
Old 02-27-2013
Oh my bad. I didn't notice you are running program in a file!

Edit your file: script.awk put only code fragment:
Code:
!/^[ \t]+Abs/ && !/^[ \t]+B-1/

To run your program:
Code:
awk -f script.awk your_input_file

Follow the same for other code fragment in script.awk:
Code:
$0 ~ /^[ \t]+Abs.*/  {
                next
} $0 !~ /^[ \t]+B-1.*/ {
                p = (p=="")?RS $0:p RS $0
} $0 ~ /^[ \t]+B-1.*/ {
                sub(/,$/,"}",p)
                print p
                p = ""
}

To run your program:
Code:
awk -f script.awk your_input_file

# 13  
Old 02-27-2013
Thanks a lot, you're a life saver! I may get to this post again, I hope you will find time to help me. Cheers!
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