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

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

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

***Disclaimer Statement *****

with blank spaces, but the sign * not replacing only the Alphabets were replaced I sued below 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

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

while replacing

FORBANK'S POLICY

thru above code

Kindly advice.

Regards,

Hassan

This is nothing particular AIX related. Moving to the Shell Scripting area.

For first question:

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:

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

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

"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.

Hi,

I have managed to remove the lines after pharse

"NUMBER OF TRANSACTIONS

thru below sed statement

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.

awk 'NR==FNR&&/PAGE             :/ {a++;next} /PAGE             :/ {$0=$0 " of " a}1' infile infile
1 Like

Dear this is displaying the changed value on screen only, as

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

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.

 
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:

      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

      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>