Recently, I have been transferring a huge file (more than 7k lines) to Ruby#Array().

Fragment:

1
2
3
4
Mozilla/5.0 (Amiga; U; AmigaOS 1.3; en; rv:1.8.1.19) Gecko/20081204 SeaMonkey/1.1.14
Mozilla/5.0 (AmigaOS; U; AmigaOS 1.3; en-US; rv:1.8.1.21) Gecko/20090303 SeaMonkey/1.1.15
Mozilla/5.0 (AmigaOS; U; AmigaOS 1.3; en; rv:1.8.1.19) Gecko/20081204 SeaMonkey/1.1.14
(...)

What I needed was:

1
2
3
4
5
6
AGENTS = [
"Mozilla/5.0 (Amiga; U; AmigaOS 1.3; en; rv:1.8.1.19) Gecko/20081204 SeaMonkey/1.1.14",
"Mozilla/5.0 (AmigaOS; U; AmigaOS 1.3; en-US; rv:1.8.1.21) Gecko/20090303 SeaMonkey/1.1.15",
"Mozilla/5.0 (AmigaOS; U; AmigaOS 1.3; en; rv:1.8.1.19) Gecko/20081204 SeaMonkey/1.1.14",
(...)
]

Modifying this manually is overkill, isn’t it? Honestly, last time I ended with copying only a few first lines and do boring manual work, but this time I decided to add functionality that allows to chose random User-Agent from the whole list.

My first thought was to use Vim macro, see here: http://vim.wikia.com/wiki/Macros

Thankfully, I found a faster and easier solution for how to wrap quickly my data.

There is sed command in Linux: https://www.geeksforgeeks.org/sed-command-in-unix/

So what I did is:

First, I copied all lines of User-Agents to empty Ruby file.

Then:

1
sed -e "s/\(.*\)/'\1'/" agents.rb > temp_quotes.rb
1
sed -e "s/\(.*\)/\1,/" temp_quotes.rb > temp_comma.rb

Finally:

1
mv temp_comma user_agents.rb

Voila!