WebHosting

Monday, January 14, 2013

Linux : Prefix a string to all rows in a file - sed

Consider, you have a file like below:

# cat test.txt
12324
234523
3431
343
34345
3434
43234


Now, if you want to prefix a particular string to each line in the file, how will you do it? This is a common challenge for DBA/Developers working with Linux environment.


Here's one of the simple way that I follow:

Say, I want to prefix below line:
SELECT * FROM testTab WHERE testCol=

So I do like this:

# cat test.txt | sed "s/^/SELECT * FROM testTab WHERE testCol=/g" > test_prefixed.sql

Now check the output file:

# cat test_prefixed.sql
SELECT * FROM testTab WHERE testCol=12324
SELECT * FROM testTab WHERE testCol=234523
SELECT * FROM testTab WHERE testCol=3431
SELECT * FROM testTab WHERE testCol=343
SELECT * FROM testTab WHERE testCol=34345
SELECT * FROM testTab WHERE testCol=3434
SELECT * FROM testTab WHERE testCol=43234



That's it!

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

OBS Browser Source Not Working? How to Interact With Web Pages Inside OBS Studio

If you're using OBS Studio to display a website, YouTube page, dashboard, live poll, chat widget, or web application, you may notice so...