user2602373
user2602373

Reputation: 3

bash script to replace spaces in html

I need a linux bash script that can replace the spaces in name="this is a test".

example:

<input name="this is a test" id="testing 1 2 3" />

would change to this:

<input name="thisisatest" id="testing 1 2 3" />

EDIT: The script must be able to match anything between the double quotes. It could be this:

<input name="THIS STRING WILL VARY" id="testing 1 2 3" />

Any ideas?

Upvotes: 0

Views: 284

Answers (4)

glenn jackman
glenn jackman

Reputation: 247002

Here's an awk one-liner

awk '
    BEGIN {FS=OFS="\""} 
    {for (f=2; f<=NF; f++) if ($(f-1) ~ /name=$/) gsub(/ /, "", $f)} 
    1
' file

It uses double quotes as the field separator. A quoted string will therefore be an odd-numbered field.

Upvotes: 0

Jon Clements
Jon Clements

Reputation: 142206

Using Python - to take an HTML file, and remove spaces from input tags that have a name attribute equal to this is a test, you can use:

from bs4 import BeautifulSoup

with open('input') as fin, open('output', 'w') as fout:
    soup = BeautifulSoup(fin.read())
    for tag in soup.find_all('input', {'name': 'this is a test'}):
        tag['name'] = tag['name'].replace(' ', '')
    fout.write(str(soup))

In response to:

I forgot to say that the string "this is a test" can be anything

You can just filter out all input tags that have a name attribute and apply whatever logic you want - the below will remove spaces from any name attribute:

for tag in soup.find_all('input', {'name': True}):
    tag['name'] = tag['name'].replace(' ', '')

Upvotes: 3

jh314
jh314

Reputation: 27802

You can use sed:

foo='<input name="this is a test" id="testing 1 2 3" />'
echo $foo | sed 's/this is a test/thisisatest/'

If you want to do this in a file and save it, you can do this:

sed 's/this is a test/thisisatest/' filename > filename

Upvotes: 0

Guy Blanc
Guy Blanc

Reputation: 861

>>> name = 'this is a test'
>>> ''.join(name.split())
'thisisatest'

Upvotes: 0

Related Questions