Edit lines in file preserving part of it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit lines in file preserving part of it
# 1  
Old 11-21-2019
Edit lines in file preserving part of it

Hello:
I have the following HTML table:
Code:
<table>
    <thead>
        <tr>
            <th>Code</th>
            <th>Percentage</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>30%</td>
        </tr>
        <tr>
            <td>2</td>
            <td>70%</td>
        </tr>
    </tbody>
 </table>

I would like to add a button with an event to every <td> element with a code, like the following:
Code:
<table>
    <thead>
        <tr>
            <th>Code</th>
            <th>Percentage</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><button onclick="reset(1)">Reset</button>1</td>
            <td>30%</td>
        </tr>
        <tr>
            <td><button onclick="reset(2)"></button>2</td>
            <td>70%</td>
        </tr>
    </tbody>
 </table>

Note that I'm not asking how to parse the HTML. Instead, I would like to know how to replace every \t\t\t<td>x</td> line by \t\t\t<td><button onclick="reset(x)">x</button></td>, where x is the code number.

I need to do it with a command-line text editor without input from the user, because I have many files like this one and my idea is to edit all of them using find with the -exec option.

This would be a simple task with ex or ed if I just had to replace the same concurrence. But in this case, I need to keep the code in every replacement. That's what I'm struggling with. I'm afraid I'll need advanced tools like awk and I honestly don't know how to do it.

How can I add the button with the event to the table cells?
Thanks in advance.
# 2  
Old 11-21-2019
Hi, with sed you can try:
Code:
sed 's|<td>\([0-9]\{1,\}\)</td>|<td><button onclick="reset(\1)">Reset</button>\1</td>|' file

You can add the leading tabs if you need them, but in the sample there were none so I left them out...
The part in red is a subexpression in escaped parentheses plus the back references to it in the replacement part...
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-21-2019
Quote:
Originally Posted by Scrutinizer
Hi, with sed you can try:
Code:
sed 's|<td>\([0-9]\{1,\}\)</td>|<td><button onclick="reset(\1)">Reset</button>\1</td>|' file

You can add the leading tabs if you need them, but in the sample there were none so I left them out...
The part in red is a subexpression in escaped parentheses plus the back references to it in the replacement part...
This is exactly what I needed, Scrutinizer. Thank you very much! The tabs don't seem to be altered with the command you provided, so I don't think I'll need to add them.

One last question. I'm using GNU sed. Can I safely use the -i option with the replacement command you wrote to do in-place editing?
# 4  
Old 11-22-2019
You are welcome Cacializ. GNU sed -i has a backup option:
Code:
-i[SUFFIX], --in-place[=SUFFIX]

The BSD sed man page says you should always use it. Anyway in general it is always a good idea to have backups and stuff like this would be best to put in a git repository and then you can just checkout the original files if something goes wrong..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

2. Shell Programming and Scripting

Preserving file format and spacing in output file

Hi I have a file with the following structure "VATTENFALL GLOBAL" "Vattenfall Tray" "BARCLAYS BANK LONDON" "Capula" "P1 AGEAS GLOBAL COMPANY" "AAC - Optiver" The requirement is like this 1) Take 2 input... (16 Replies)
Discussion started by: sudeep.id
16 Replies

3. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

4. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

5. Shell Programming and Scripting

Preserving Empty Lines while Replacing Numbers

Greetings, I am using tcsh to write a script that will replace the numbers in a file with a single number, the caveat is that this file has blank lines which are necessary for another step down the line so I need to preserve the blank lines. I have tried sed and awk but both will collapse the... (1 Reply)
Discussion started by: bonesy
1 Replies

6. Shell Programming and Scripting

Help in preserving special characters from input file

Hi Forum. I've tried to search online for a solution but I cannot seem to find one. Hopefully, someone here can help me out. I would appreciate it. Input file abc.txt: $InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst... (14 Replies)
Discussion started by: pchang
14 Replies

7. Shell Programming and Scripting

How to edit file sections that cross multiple lines?

Hello, I'm wondering where I could go to learn how to edit file sections that cross multiple lines. I'm wanting to write scripts that will add Gnome menu entries for all users on a system for scripts I write, etc. I can search an replace simple examples with sed, but this seems more complex. ... (8 Replies)
Discussion started by: Narnie
8 Replies

8. Shell Programming and Scripting

To Read a File and Insert a part of the lines into the database

Hi Guys I need to have a shell script which reads a log file and insert a part of each line into the database. Some sample lines in the file are as below. 20091112091359 MED_RQACK : user_data=60173054304,100232120,20091112091359,;ask_status=0;ask_reason=OK;msg_id=20091112091319... (5 Replies)
Discussion started by: Somanadh
5 Replies

9. Shell Programming and Scripting

Using cp: preserving file/folder attributes and auto creating folders

Hi, Is there a way to use cp in such a way that when a file is copied to a destination, the required destination folders are automatically created with the proper permissions, and the resulting copied file has the same attributes as the original. For example if I copied... (1 Reply)
Discussion started by: pcwiz
1 Replies

10. Shell Programming and Scripting

Edit number of lines in a file to single line

Greetings, I have a file: hostnames.txt which has - # cat hostnames.txt machine1 machine2 I need the output to be saved to a variable as: HOSTNAMELIST=machine1,machine2 Please advise. Thanks, Chiru (3 Replies)
Discussion started by: chiru_h
3 Replies
Login or Register to Ask a Question