insert word in each line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert word in each line of a file
# 1  
Old 03-22-2006
insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file.
i tried with sed but didn't managed yet.
Is there another command or this one(sed) works?
10x anyway.
This User Gave Thanks to atticus For This Post:
# 2  
Old 03-22-2006
Quote:
Originally Posted by atticus
can someone tell me how can I insert a word in front of each line in a file.
i tried with sed but didn't managed yet.
Is there another command or this one(sed) works?
10x anyway.
what have you tried exactly?
# 3  
Old 03-22-2006
i've tried
sed i \ txtinsert filename
# 4  
Old 03-22-2006
Code:
sed 's/^/newBeginning/' filename

# 5  
Old 03-22-2006
Quote:
Originally Posted by vgersh99
Code:
sed 's/^/newBeginning/' filename

could u pls expain to me what means the expr 's/^/newBeginning/' ?
# 6  
Old 03-22-2006
Quote:
Originally Posted by atticus
could u pls expain to me what means the expr 's/^/newBeginning/' ?
doing 'man sed' yields:
Code:
s/regular expression/replacement/flags
     ____________________________________________________________
                  Substitute   the   replacement   string    for
                  instances  of  the  regular  expression in the
                  pattern  space.   Any  character  other   than
                  backslash  or newline can be used instead of a
                  slash to delimit the RE and  the  replacement.
                  Within the RE and the replacement, the RE del-
                  imiter itself can be used as a literal charac-
                  ter if it is preceded by a backslash.
     ____________________________________________________________
                  An ampersand (&) appearing in the  replacement
                  will  be  replaced  by the string matching the
                  RE.  The special meaning of & in this  context
                  can   be   suppressed   by   preceding  it  by
                  backslash.  The characters \n, where  n  is  a
                  digit, will be replaced by the text matched by
                  the  corresponding  backreference  expression.
                  For each backslash (\) encountered in scanning
                  replacement from beginning to end, the follow-
                  ing  character  loses  its special meaning (if
                  any).  It is unspecified what special  meaning
                  is  given  to any character other than &, \ or
                  digits.

s/^/newBeginning/

's' - substitute
'^' - beginning of the line

Last edited by vgersh99; 03-22-2006 at 05:05 PM..
# 7  
Old 03-22-2006
10x a lot for help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

5. Shell Programming and Scripting

How to insert a word into a text file?

Hi, I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that? It seems we can do using awk but unable to figure out. Please advise, thanks! ... (11 Replies)
Discussion started by: magnus29
11 Replies

6. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

7. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

8. Shell Programming and Scripting

to insert some word somewhere in the line with shell (or perl)

hello mighty all there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end.. my own knowledge base can say it is... (12 Replies)
Discussion started by: tip78
12 Replies

9. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question