LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to insert texts in a file by using shell? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-insert-texts-in-a-file-by-using-shell-858719/)

masterav 01-26-2011 04:15 AM

how to insert texts in a file by using shell?
 
Hi,

I'm using fedora 14...i would like to insert few texts to a file in shell prompt(may be useful in scripting for in the future)...for eg,

[root@ruby ~]# cat > hello
1
2
2
3
4
5
6
7
8
9
10
[root@ruby ~]#

so in the above file i need to add the line "new line added" near the number 4 in the above file hello.....how can be this done ...?



Thanks in advance....

chickenjoy 01-26-2011 04:35 AM

the word 'near' is very vague; a lot of questions will arise like:

- near where? above? bottom?
- why number 4? is it because you want to add it to every 4th line? or each time it sees the number "4"?
- etc.

colucix 01-26-2011 04:40 AM

Using sed you can do:
Code:

sed '/4/s/$/ new line added/' file
this substitutes the end of the line (that is append at the end of the line) containing 4 the string new line added. Output will be sent to standard output. If you really want to change the file, use option -i (see man sed for details).

Instead, if you want to add a new line before, use:
Code:

sed '/4/i new line added' file
to add a line after:
Code:

sed '/4/a new line added' file
Let me give a little advice for a good reading about sed: http://www.grymoire.com/Unix/Sed.html. Hope this helps.

catkin 01-26-2011 04:43 AM

Duplicate thread reported. Original is http://www.linuxquestions.org/questi...-shell-858721/. Better to keep all replies in original until moderators can merge the threads.

colucix 01-26-2011 04:57 AM

Quote:

Originally Posted by catkin (Post 4238054)
Duplicate thread reported. Original is http://www.linuxquestions.org/questi...-shell-858721/. Better to keep all replies in original until moderators can merge the threads.

Hi catkin! :) Well.. based on the timestamp of the OP, this one should be the original. BTW, it doesn't matter if the threads are merged, since the replies will be sorted by date and time.

kurumi 01-26-2011 05:13 AM

Code:

ruby -ne 'print /^4/?$_+"insert text\n":$_' file

masterav 01-26-2011 07:04 AM

Hi all,


First my reply to Chickenjoy---------->sorry for the incomplete information,i just want to add it below 4th line for this file only.And i think you need not be confused with other questions you have raised.


Second to Mr.Coucix,--------->I've tried you suggestions but the entries are not permanent,I'll show it below,


[root@ruby ~]$ sed '/4/i new line added/' hello

1
2
3
new line added/
4
5
6
7
8
9
10

5:hekkpo
[root@ruby ~]$ sed '/4/s/$/ new line added/' hello

1
2
3
4 new line added
5
6
7
8
9
10

5:hekkpo
[root@ruby ~]$ cat hello

1
2
3
4
5
6
7
8
9
10

5:hekkpo
[root@ruby ~]$

Third to Mr.Kurumi--------->I'll show the output

[root@ruby ~]$ ruby -ne 'print /^4/?$_+"insert text\n":$_' hello
bash: ruby: command not found
[root@ruby ~]$




Thanks my friends for your suggestions till now,

colucix 01-26-2011 07:19 AM

Sorry but it is not clear if you want to add some text below the 4th line or below the line containing the number 4 (which is the 5th line in your first example).

In the first case you can tell sed to apply the 'a' command to the 4th line only:
Code:

sed '4a new line added' file
whereas to match the number 4 as a pattern, one of my previous examples can be applied:
Code:

sed '/4/a new line added' file
If you read carefully my previous post, I already told that in order to actually change the content of the file, you have to use the -i option.

masterav 01-27-2011 10:17 AM

Thanks Colucix
 
Hi,

It is working for me now


Thanks a lot....


All times are GMT -5. The time now is 02:26 PM.