Adding one string at the beginning of each line in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding one string at the beginning of each line in a file
# 1  
Old 08-12-2009
Adding one string at the beginning of each line in a file

Hi,

I have file a.txt as below. I want to add one string root beginning of each line.

Sample file a.txt
aaa
bbb
ccc

Sample output
Root aaa
Root bbb
Root ccc
Can any one help me on this?
# 2  
Old 08-12-2009
Code:
for i in $(cat a.txt); do echo "Root $i"; done

actually, probably better with awk
Code:
awk '{print "Root " $0}' a.txt

Or with sed
Code:
sed 's/^/Root /g' a.txt

BTW, I'm not showing off, I'm trying to improve my sed and awk skills and am using your question to try them out Smilie

Last edited by mglenney; 08-12-2009 at 01:58 PM..
This User Gave Thanks to mglenney For This Post:
# 3  
Old 08-12-2009
Tools you can write a new file with awk

Code:
MYVAR="Root "
awk -v MYVAR=$MYVAR '{print MYVAR, $0}' a.txt >b.txt

# 4  
Old 08-13-2009
Quote:
Originally Posted by mglenney
Code:
for i in $(cat a.txt); do echo "Root $i"; done

actually, probably better with awk
Code:
awk '{print "Root " $0}' a.txt

Or with sed
Code:
sed 's/^/Root /g' a.txt

BTW, I'm not showing off, I'm trying to improve my sed and awk skills and am using your question to try them out Smilie
Really It's helpful. Actually I was expecting the solutions with the use sed or awk. Thanks a lot.....
# 5  
Old 01-06-2010
One question:
If I use sed from the command line. It will serve the purpose. But It wont save it in the file. It will just display it on the screen.
Then I tried to redirect the output to the same file. In this example, below is the command I did. And this command made file empty.

sed 's/^/Root /g' a.txt > a.txt

Why is this SED not redirecting to the same file?
# 6  
Old 01-06-2010
sed -i 's/^/Root /g' a.txt

-i option will do inplace modification.
# 7  
Old 01-06-2010
Thanks Sank.
Thanks is what I want.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding test to beginning and end of list in a file

Hi all and apologies for the silly question, but I've searched and I can't get this right. I have a list of email addresses in a file that I need to blacklist (spam). the list is quite long and I would need to script a small routine so that I can get the following for each line in the file: db... (4 Replies)
Discussion started by: bm555
4 Replies

2. Shell Programming and Scripting

Put a string to the beginning of a file without a linefeed

Hello, I need to put the following string to the beginning of a file - but it should not create a newline at the end of the string. So, the file I have is a compressed one - with gzip. And I would like to add an ASCII-String to the beginning of the file. The string has a length of 69... (5 Replies)
Discussion started by: API
5 Replies

3. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

4. Shell Programming and Scripting

Adding a text in the beginning of a line

Hi, I am doing something like below: cat file1>file3and cat file2>>file3 I wanted to check if there is a way to write a custom message(hardcoded message)something like below at the beginning of each line then PIPE delimitiation and then followed by remaining record. cat file1... (7 Replies)
Discussion started by: Saanvi1
7 Replies

5. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

6. UNIX for Dummies Questions & Answers

rename file by adding a name in beginning

about 200 files with names as 11_0.pdb 11_60.pdb 12_12.pdb 14_180.pdb are there in my library...I need to rename all of them by addinf File in front of them as: File11_0.pdb File11_60.pdb File12_12.pdb File14_180.pdb I checked many threads but it seems i get even more confused..i... (1 Reply)
Discussion started by: kanikasharma
1 Replies

7. Shell Programming and Scripting

[string processing]Adding new line in file

I have a report file which somewhere has a lines starting with word ".sub" I have a new variable named $new. What i am trying to do is insert the content of $new just before the occurrence of ".sub" in the report file. How can I do that? (11 Replies)
Discussion started by: animesharma
11 Replies

8. UNIX for Dummies Questions & Answers

write new line at the beginning of an existing file

I was trying to find out the easiest way to write new line to the beginning of an exisiting file. I am using KSH. (5 Replies)
Discussion started by: sailussr
5 Replies

9. Shell Programming and Scripting

input a line at the beginning of every file in a directory?

if need to input a word or anything at the beginning of every file in a directory. how do i accomplish this? say the file is named hyperten. how do i make hyperten the first line of every file in a given directory? thanks (6 Replies)
Discussion started by: Terrible
6 Replies

10. Shell Programming and Scripting

Adding a character in the beginning of every line in a .dat file

How can i add a character(#) in the beginning of every line in a .dat file (2 Replies)
Discussion started by: Cool Coder
2 Replies
Login or Register to Ask a Question