Find and add/replace text in text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and add/replace text in text files
# 1  
Old 02-10-2011
Find and add/replace text in text files

Hi.

I would like to have experts help on below action.
I have text files in which page nubmers exists in form like
Code:
PAGE             :   1
PAGE             :   2
PAGE             :   3

and so on there is other text too. I would like to know is it possible to check the last occurance of Page numer and replace this value as below
Code:
PAGE             :   1 of 3
PAGE             :   2 of 3
PAGE             :   3 of 3

Similarly I have to replace some text with blank spaces.
i.e the text is like ***Disclaimer Statement**** . Can I replace this with spaces. This text is Fixed and always occured in the end of every text file.

Regards,

Hassan

---------- Post updated at 02:53 PM ---------- Previous update was at 11:38 AM ----------

Further to update that I have tried to replace the text
Code:
***Disclaimer Statement *****

with blank spaces, but the sign * not replacing only the Alphabets were replaced I sued below code.
Code:
 
 cd /text
find /text -name 'c*.txt' |
while read filename
do
cat $filename | sed 's/****Disclaimer Statement *****\(\)/\1/' > temp.txt
mv temp.txt $filename
done

Similarly received error
Code:
0403-057 Syntax error at line 9 : `'' is not matched.

while replacing
Code:
FORBANK'S POLICY

thru above code


Kindly advice.

Regards,

Hassan

Last edited by radoulov; 02-10-2011 at 05:19 AM.. Reason: Code tags, please!
# 2  
Old 02-10-2011
This is nothing particular AIX related. Moving to the Shell Scripting area.
# 3  
Old 02-10-2011
For first question:

Code:
awk 'NR==FNR{a[$1]++;next} /^PAGE/ {$0=$0 " of " a["PAGE"]}1' infile infile

PAGE             :   1 of 3
PAGE             :   2 of 3
PAGE             :   3 of 3

replace by space:

Code:
sed "s/\*\*\*Disclaimer Statement \*\*\*\*\*/ /" infile

# 4  
Old 02-11-2011
Question Find and add/replace text in text files

Hi rdcwayx,

I have tried but the page no reformatting is not working.
I have uploaded the sample file names "test.txt". Kindly note that the contents are purely dummy.


Similarly the second option's approach has changed. Now I want to replace all the text with blank lines/spaces after pharase
Code:
"NUMBER OF TRANSACTIONS"



Actually the text might be changed after time to time after this pharse, so I need to check how many lines are there having text and remove all lines with blank lines. In attachment you can view that there is 13 lines after this pharse that contains any text I want to replace these 13 lines (or any number of lines if text changes) with blank lines and the reamining blank lines after and before that text must be same.



Thanks.
# 5  
Old 02-11-2011
Hi,

I have managed to remove the lines after pharse
Code:
"NUMBER OF TRANSACTIONS

thru below sed statement
Code:
sed '/NUMBER OF TRANSACTIONS/q' infile > outfile

.
Now I need to add a page break or add nubmer of lines that were deleted after this pharase.
# 6  
Old 02-11-2011
Code:
awk 'NR==FNR&&/PAGE             :/ {a++;next} /PAGE             :/ {$0=$0 " of " a}1' infile infile

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 02-11-2011
Question

Dear this is displaying the changed value on screen only, as
Code:
PAGE             :   1 of 5
PAGE             :   2 of 5
PAGE             :   3 of 5
PAGE             :   4 of 5
PAGE             :   5 of 5

but the text file remain same as
Code:
PAGE             :   1
PAGE             :   2
PAGE             :   3
PAGE             :   4
PAGE             :   5

I need to update the changed value in text file as well.

---------- Post updated at 12:22 PM ---------- Previous update was at 11:54 AM ----------

Hi,

I have managed to add page breaks at the end of file with below code.
Code:
echo ^L >> file name

^L: is the combination of Ctrl+v and Ctrl+L



Now if gurus can help me regarding change the value of Page numbers.


---------- Post updated at 01:11 PM ---------- Previous update was at 12:22 PM ----------

Gursu:

If you can help me to re-format the initial 5 lines of text file on every page as below:

Original Text Format:
Code:
      TITLE OF ACCOUNT : HASSAN RAZA KHAN                                                                       STATEMENT DATE   :  31/01/2011 23:43:26
      HASSAN RAZA KHAN                                                                                          STATEMENT PERIOD :  01-JUL-10 - 31-JAN-11
      PLOT ST-02 FAYSAL HOUSE                                                                                   A/C NO. TYPE/CCY :  07777777777777 149/PKR
      ST-02 SHAHRAH-E-FAISAL                                                                                    PAGE             :   5
      KARACHI, SINDH, PAKISTAN - 5321

Desired Text Format
Code:
      STATEMENT DATE   :  31/01/2011 23:43:26
      STATEMENT PERIOD :  01-JUL-10 - 31-JAN-11
 
      HASSAN RAZA KHAN
      A/C NO. TYPE/CCY :  07777777777777 149/PKR
      PAGE : 1 of <No. of Pages>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

How to find text and replace next line.?

Have multiple files with the same format and certain text is the same. Have specific text to search for and then need to replace the next line. Can search on the text DEVICE and multiple lines will be found. The line after each DEVICE line has {. Want to replace the line { with {-someadditiontext.... (2 Replies)
Discussion started by: bigdaddywags
2 Replies

3. Shell Programming and Scripting

How to add a text at the beginning of a text files in a folder?

how to add a text ( surya) at the beginning of a text files (so many) in folder text file: 111111 555555 666666 result: surya 111111 555555 666666 (3 Replies)
Discussion started by: suryanarayana
3 Replies

4. Shell Programming and Scripting

Find and replace using 2 text files as arrays.

Here's the nonfunctional code I have so far #!/bin/bash searchFor=(`cat filea.txt` ) replaceWith=(`cat fileb.txt`) myMax=${#searchFor} myCounter=1 while ; do sed -i 's/${$searchFor}/${$replaceWith}/g' done The goal is to use each line in filea.txt as a search term, and each line... (2 Replies)
Discussion started by: Erulisseuiin
2 Replies

5. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

6. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

7. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

8. UNIX for Dummies Questions & Answers

Find and replace text PLEASE HELP

Dear friends please help, I have a large data file with few hundred lines. A small example is shown below: datafile is a file with few hundred lines with the third column has many different character stings: test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh I need to... (6 Replies)
Discussion started by: bobo
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace text

test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh Please advise how can I replace the abc, bcd....with ABC, BCD.... (1 Reply)
Discussion started by: bobo
1 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies
Login or Register to Ask a Question