Removing a string of text from a file - help please


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing a string of text from a file - help please
# 1  
Old 02-14-2010
Removing a string of text from a file - help please

Hey Folks,

I have a file that contains data that I am working with, sometimes this file has a very long string of text that messes with an awk command in a script i am trying to build. I would like to cut this string of text out of a file and then redirect everything except that string to a new file...

Input file:
Code:
/opt/hd/pt/var/tmp/${store}t_taxware.xml

Output file:
Code:
/opt/hd/pt/var/tmp/${store}_taxware.xml

the file is in XML format of sorts.. this is an example of the data in the file..
Code:
<tax><RepCents><Rep_0>10.00</Rep_0><Rep_1>10.00</Rep_1><Rep_2>10.00</Rep_2><Rep_3>10.00</Rep_3><Rep_4>10.00</Rep_4>
<Rep_5>10.00</Rep_5></RepCents></tax>

I would like to cut everything between <RepCents> and </RepCents> but leave everything else in the file intact.

can anyone help here, thank you very much for your time.. Smilie

Last edited by Scott; 02-14-2010 at 02:20 PM.. Reason: Code tags
# 2  
Old 02-14-2010
try this
Code:
# cat infile.xml
<tax><RepCents><Rep_0>10.00</Rep_0><Rep_1>10.00</Rep_1><Rep_2>10.00</Rep_2><Rep_3>10.00</Rep_3><Rep_4>10.00</Rep_4>
<Rep_5>10.00</Rep_5></RepCents></tax>


# awk '{ sub("<RepCents>[[:print:]]*</RepCents>","") ; print} '  infile.xml

 <tax></tax>

# 3  
Old 02-14-2010
Thanks for the reply... I cannot use AWK because the string of text is over 3,000 bytes.. I get this error...

Code:
/opt/tmp/pt>cat st0366t_taxware.xml | awk '{ sub("<RepCents>[[:print:]]*</RepCents>","") ; print} ' > st0366_taxware.xml

This is what I get

awk: Input line </TaxLaws></XMLData> cannot be longer than 3,000 bytes.
The input line number is 168.
The source line number is 1.
# 4  
Old 02-14-2010
Try with Perl

Code:
cat 
infile.xml | perl -e 'while (<>){ chomp;  s/\<RepCents\>.*?\<\/RepCents\>//g; print "$_\n";}'

HTH,
PL

Last edited by radoulov; 02-15-2010 at 05:34 AM.. Reason: Please use code tags!
# 5  
Old 02-15-2010
Quote:
Originally Posted by deepslp
This is what I get

awk: Input line </TaxLaws></XMLData> cannot be longer than 3,000 bytes.
The input line number is 168.
The source line number is 1.
try gawk ( since gawk has no pre-defined limits.)
Code:
GAWK(1)                  Free Software Foundation                   GAWK(1)
 Utility Commands
       -mf NNN
      -mr NNN
           Set various memory limits to the value NNN.  The f flag sets the
           maximum number of fields, and the r flag sets the maximum record
           size.  These two flags and the -m option are from an earlier
           version of the Bell Laboratories research version of UNIX awk.
           They are ignored by gawk, since gawk has no pre-defined limits.
           (Current versions of the Bell Laboratories awk no longer accept
           them.)

# 6  
Old 02-15-2010
Bummer - we do not have gawk... but the perl script worked.. thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing string from CSV file by provide removal string from other file

What I need is to remove the text from Location_file.txt from each line matching all entries from Remove_location.txt Location_file.txt FlowPrePaid, h3nmg1cm2,Jamaica_MTAImageFileFlowPrePaid,h0nmg1cm1, Flow_BeatTest,FlowRockTest FlowNewTest,FlowNewTest,h0nmg1cm1 PartiallySubscribed,... (3 Replies)
Discussion started by: ketanraut
3 Replies

2. UNIX for Dummies Questions & Answers

Removing \n from a text file

Hi All, I have a question regarding text substitution. I have a file that contains a lot of text. Some of the text is separated with a \n like: TEST\nTEST2\nTEST3 BLA\nBLA2\nBLA3 So there are both actual newlines and 'used to be newlines' in the text. using tr tr "\n" "," or... (5 Replies)
Discussion started by: JaapSchuurman
5 Replies

3. Windows & DOS: Issues & Discussions

Removing anything from text file except specific string

So, I have a text file that looks like this: 0,0: (168,168,176) #A8A8B0 srgb(168,168,176) 1,0: (168,168,176) #A8A8B0 srgb(168,168,176) 2,0: (166,166,174) #A6A6AE srgb(166,166,174) 3,0: (166,166,174) #A6A6AE srgb(166,166,174) 4,0: (168,168,176) #A8A8B0 srgb(168,168,176) 5,0:... (0 Replies)
Discussion started by: pasc
0 Replies

4. Shell Programming and Scripting

Removing Carriage return in a file after particular string

Hi All, I want to remove carriage return in a file using some unix command without writing a script my file is as follows abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 I want the output as follows: abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 , Please... (7 Replies)
Discussion started by: manish8484
7 Replies

5. Shell Programming and Scripting

Removing zero values from text file

Hi all, I wrote the following code to remove the value which are 0 in the input file (a columns if numbers). awk 'BEGIN { for (i=1; i<=NF; i++) if ($i) printf("%13.6e\n",$i) }' $1 >> $2 The script works if the zeros are written as 0.0000 but not as 0.000000e+00 In... (10 Replies)
Discussion started by: f_o_555
10 Replies

6. Shell Programming and Scripting

Removing last occurance of string in text

I have text file as follows and would like to remove the last occurance of "UNION ALL" string and replace @@ with single quote ('). Input text in file is with temp as ( ( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL ( select... (9 Replies)
Discussion started by: Vaddadi
9 Replies

7. Shell Programming and Scripting

Removing text from a line in a file

Hi All, I would like to know how to remove text from a line in a file. eg to The 4 sets of numbers are not static ie they change on each line in each different file so if anyone can help that would be great. Jeremy (10 Replies)
Discussion started by: outthere_3
10 Replies

8. Shell Programming and Scripting

Removing a particular line from a text file

Hi, I have a file called inp.txt the contents of the file are as follows MANI123|23|41 MANI123|96|23 I want to reove the first line of this file. How can I do it. Thanks in advance (5 Replies)
Discussion started by: sendhilmani123
5 Replies

9. Shell Programming and Scripting

Removing lines in a text file.

Here is my problem I'm hoping you guru's can help me figure out. I have a text file that contains comma delimited columns. What I'm looking to do is see if the 24th column on each row in the file contains a value (not null), and then write/append that line to a different file. I've been... (4 Replies)
Discussion started by: WABonnett
4 Replies

10. UNIX for Dummies Questions & Answers

removing commas from text file

Dear all I have a file which looks like this xxxxxxxxxxxxxx,xxx,xxxxxxxxxx xxxxxxxxxxxxxx,xxx,xxxxxxxxxx etc basically 14 characters then a comma, three characters, then a comma then 10 characters. We are uploading this file to our mainframe and they want the commas removed, so it... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question