sed 's/^/You /' test.txt > new-test.txt and awk '{print "You "$0}' testfile.txt What is wrong?


sed 's/^/You /' test.txt > new-test.txt and awk '{print "admin "$0}' testfile.txt
Thru much reading and research. I have found that with awk and sed I can prepend a word and 4 spaces (much rather tab) at the beginning of each line. However it is not consistent. I get either one or both of the examples below. And this will occur in the same file.

Ex 1

Ex 2


(Notice) When I typed this the words were out of line some. However when I posted this it straightened them up.

Now am I doing something wrong? Which would be more able to do this job best? One last thing, I can get it to write a new file. Is there a way to write to a new file and to console so I know here it is so I have an idea how much longer it will take?


Well-Known Member
Can you clarify what you're trying to do and what the problem is?
Your post isn't very clear.

If you post any commands and/or example outputs in between code tags:
# Your code here
Then the formatting of what you copy/paste should be retained.

And then when you publish your post - your code will appear inside a neat box like this:
# your code here
If you don't fancy typing the tags manually - there is also the "..." button in the post-editors toolbar. This is the "insert" button.

To insert code into your post:
Click the insert button ("...") and select the "</> Code" item in the menu that pops up.
That will pop up a little dialog that has a couple of controls on it.
The first will allow you to select the type of code you are trying to post (e.g. bash, C/C++, Python etc) the second is a large text area inside which you can copy/paste your code.

When you're happy with your code - press the continue button and the editor will automagically insert your code into your post - inside code-tags.

Getting back on-topic and going back to what you previously posted:
The sed and awk commands you've posted are both valid and work.
The sed command will go through test.txt and will pre-pend "You " to the start of every single line and output to a new file called new-test.txt.

Your awk command will read each line in testfile.txt and prints "admin " before the start of every line. But that command only displays the output in the terminal. It doesn't change the file.

To write the output from the awk command to a new file, you can use file redirection:
awk '{print "admin "$0}' testfile.txt > testfile2.txt
OR if you want the awk command to display its output in the terminal AND write to a file, then you can pipe the output to the tee command:
awk '{print "admin "$0}' testfile.txt | tee testfile2.txt
Regarding the other two examples at the end of your post - I'm not sure exactly what you mean here. Again, please try pasting that output between some code-tags and then it should retain it's original formatting in your post.

Without code tags - linux.orgs post editor will strip out any leading/trailing whitespace, removing any indentation. And with certain programming languages, like C and C++ any array indices using i as a variable will cause the rest of the text in your post to be italicised. It can cause other side-effects in your post too.

So pro-tip - whenever posting code-snippets/scripts, or program output here on - it's a good idea to paste them inside code-tags when you write your post.


This morning a couple of hours ago I got it working and it was not code related. The sed was based on snippets you gave me and I was able to figure out the rest and made it work. I am gaining an understanding on how sed, awk, bash work. I am still no pro and a long while before I will be. But thanks to you guys and some books I am read, I am picking it up.

So lets consider this closed. You are correct about the proper manner to post the code. I was aware of that however I wasn't thinking. Thank you for the reminder.

Members online

Latest posts