sed to add text in new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to add text in new line
# 1  
Old 01-09-2013
sed to add text in new line

help

i need to add a "nfsd" in new line after cron

ex:
Code:
cron
rpcbind

output:
Code:
cron
nfsd
rpcbind

i use
Code:
sed -e "/cron/G; s/$/nfsd/" myfile

output:
Code:
cron
nfsd
rpcbindnfsd


help!!!

Last edited by Franklin52; 01-10-2013 at 03:19 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 01-10-2013
try:
Code:
sed "/cron/n;i nfsd" myfile

# 3  
Old 01-10-2013
rdrtx1's solution will work only on GNU sed, if you do not have it then use awk:
Code:
awk '$0=="cron"{$0=sprintf("%s\n%s",$0,"nfsd");}1' filename

# 4  
Old 01-10-2013
try this
Code:
sed 's/\(cron\)/\1\nnfsd/g' filename

# 5  
Old 01-10-2013
Code:
 
$ sed 's/cron/cron\
nsfd/' input.txt

type \ and press enter and type nsfd....
# 6  
Old 01-10-2013
thx all...
awk is working ..
Code:
 
awk '$0=="cron"{$0=sprintf("%s\n%s",$0,"nfsd");}1' filename

Code:
 
awk '$0=="cron"{$0=sprintf("%s\n%s",$0,"nfsd");}1' filename>filename1

love U all | warpiece
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 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

How to identify exact text and then add a blank line above it using sed?

I need to identify the exact text of San Antonio Generator Running in the output my script which lands to a text file. Once SED finds the specific text, I need it to insert one line above the matched text. Here is what I have so far that isn't working all that well for me. Any help would be... (7 Replies)
Discussion started by: jbrass
7 Replies

4. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 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

sed add after line x new text from file

I've been playing with sed, trying to get it to insert the contents of somefile.txt after line 13 on anotherfile.txt. I tried searching for a line with regex and attempting to insert something on the next line with: find ./anotherfile.txt -type f -exec sed -i -e '/^dog/cat/' {} \; but it... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

Text cut between two $ in a line with SED

Let's say I have a line like that: I want to cut out numbers between two $ including $s. The result should be like that: I am so-newbei. I am non-stop reading about SED since yesterday and not a programmer. I know that it is a short period, thus maybe I had overlooked something. I... (4 Replies)
Discussion started by: l_p
4 Replies

9. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

10. Shell Programming and Scripting

sed.. Add new line after finding text

I know that this my be really simple, but I'm having a hard time accomplishing it. I am trying to add a new line of text after finding a particular string of text in a file. Here's what I'm getting: sed: command garbled: N/search_string/\\new_text/ I was using "N" to add a line after the... (3 Replies)
Discussion started by: douknownam
3 Replies
Login or Register to Ask a Question