Add text to specified line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add text to specified line
# 1  
Old 06-14-2012
Add text to specified line

Hi All,

I am writing a script in which i need to add text "ABC" at line 14 in file input.txt

Can someone please help me how to do it? Thanks.

Regards,
Tushar
# 2  
Old 06-14-2012
What have you tried and what kind of errors did you get?
# 3  
Old 06-14-2012
I don't have much idea how file editing can be done within a shell script. I googled for it and came to know some commands like awk and ed can be useful but not sure what would be the exact command for doing this. Thanks.
# 4  
Old 06-14-2012
Add text to specified line

Hello,

Try this.

Code:
 
awk '{s=$0; if( NR==14 ){ s=s " ABC" } print s;}' inputfile > outfile

Modify accordingly..

Thanks
Krsnadasa
# 5  
Old 06-14-2012
Hi tushar_shah06,

One way using perl (change number 3 with your line number):
Code:
$ cat infile
one
two
three
four
five
six
seven
eigth
nine
$ perl -lpe 'substr $_, length , 0, q/ ABC/ if $. == 3' infile
one
two
three ABC
four
five
six
seven
eigth
nine

# 6  
Old 06-14-2012
Code:
awk 'NR==14{$0 = $0 "TEXT"}1' file

# 7  
Old 06-14-2012
Add text to specified line

Hi,

Why not this?

Code:
sed with option i: - 
 
sed "Lineno i Notes : Paste Here " file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add text to the end of line

Seems simple but ive been searching for a good hour of so I have a text file and would like to add a string to the end of line 5 ( as an example) to ake tings hard the line number we have to add the text to is stored in a variable cunningly name $Line_to_append any ideas on how this could... (2 Replies)
Discussion started by: dunryc
2 Replies

2. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

3. Shell Programming and Scripting

sed to add text in new line

help i need to add a "nfsd" in new line after cron ex: cron rpcbind output: cron nfsd rpcbind i use sed -e "/cron/G; s/$/nfsd/" myfile output: cron nfsd rpcbindnfsd (5 Replies)
Discussion started by: jamilzain
5 Replies

4. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

5. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

6. Shell Programming and Scripting

how to add text as the new first line without getting ^M?

hi all, first post here how to add text as the new first line without getting the "^M"s in vim? I've tries echo "new_test" cat "file_to_be_edited" > new file and sed -i '1i\ new_test' file_to_be_edited these two can successfully add new_test as the new first line, but both of them... (3 Replies)
Discussion started by: sunnydanniel
3 Replies

7. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

8. Shell Programming and Scripting

Add ; to every line in text file

Please help to add ; to every line in a text file i Have tired sed 's/$/ ; /g' > /tmp/drop_tables.sql but not working :( Thanks (2 Replies)
Discussion started by: bluebird5m
2 Replies

9. Shell Programming and Scripting

how to add text after a particular line on vi editor

hi suppose i have file 0f 10 lines. i want to add something after 8 line. may i append it anyhow without opening file. eg. i can appned at last. echo text >> file Thanks (9 Replies)
Discussion started by: manoj_dahiya22
9 Replies

10. Shell Programming and Scripting

Add text to file at a certain line

I am trying to add a line of text just before the last line in a file. For example, if the last line of a file is "exit 0", I need to add a line of text just before that. Any ideas how I might do that? Thanks (5 Replies)
Discussion started by: TheCrunge
5 Replies
Login or Register to Ask a Question