LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-26-2011, 04:15 AM   #1
masterav
Member
 
Registered: Aug 2008
Location: India
Distribution: redhat el5,fedora,centos
Posts: 30

Rep: Reputation: 0
Question 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....
 
Old 01-26-2011, 04:35 AM   #2
chickenjoy
Member
 
Registered: Apr 2007
Distribution: centos,rhel, solaris
Posts: 200

Rep: Reputation: 30
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.
 
Old 01-26-2011, 04:40 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 01-26-2011, 04:43 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.
 
Old 01-26-2011, 04:57 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by catkin View Post
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.
 
Old 01-26-2011, 05:13 AM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
Code:
ruby -ne 'print /^4/?$_+"insert text\n":$_' file
 
Old 01-26-2011, 07:04 AM   #7
masterav
Member
 
Registered: Aug 2008
Location: India
Distribution: redhat el5,fedora,centos
Posts: 30

Original Poster
Rep: Reputation: 0
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,

Last edited by masterav; 01-26-2011 at 07:08 AM. Reason: data exposed
 
Old 01-26-2011, 07:19 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 01-27-2011, 10:17 AM   #9
masterav
Member
 
Registered: Aug 2008
Location: India
Distribution: redhat el5,fedora,centos
Posts: 30

Original Poster
Rep: Reputation: 0
Cool Thanks Colucix

Hi,

It is working for me now


Thanks a lot....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to insert value in database deepu_linux Programming 14 04-25-2013 12:07 PM
How to get texts and image of texts on ubuntu terminal like linux mint? cola Ubuntu 1 06-02-2010 12:01 AM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 04:37 AM
SHELL Script to insert a line. santhoshv Programming 9 04-03-2008 06:10 AM
shell script: insert line in a file noir911 Programming 6 02-04-2008 10:42 PM


All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration