Simple sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple sed command
# 1  
Old 11-06-2006
Simple sed command

Hi,

I have the following file:

--#
--#line1
--#line2
--#line3
--#
--#line4
--#line5

and I want to use something like:

sed 's/--#/newline/g' file > newfile

to substitute the lines containing only '--#', but when I try, it replaces every instance of '--#' with 'newline' and I want to keep the lines with text after '--#'!

Does that make sense?

Can anyone help!?
# 2  
Old 11-06-2006
Try this:
Code:
sed s/^--#$/newline/ file > newfile

Regards.
# 3  
Old 11-06-2006
Thanks that worked great!

Is there any method of apply this to only the second line containing only '--#' and leaving the first line the same?

Thanks for your help!
# 4  
Old 11-06-2006
Code:
sed "2{s/^--#$/newline/;}" file > newfile

# 5  
Old 11-06-2006
Thank you, that works on the first occurence if i change the 2 at the start to a 1, but it doesnt work on the 2nd occurence if it is left as a 2!

Im not sure why!
# 6  
Old 11-06-2006
Oh I see, it works if I change the number to 9 (The line number of the second '--#'.

But I have many files and the 2nd occurnce of '--#' occurs on different lines in each file.

Is there anyway to applu the command to the second occurence instead of specifying a line number?
# 7  
Old 11-06-2006
Ok,
Ive found out what I need to do, I need to expand the pattern space to include the entire file (not just one line), then search for the nth occurence of '--#'.

Does anyone know how to do this?

Ive tried this:
sed -n -e H -e '${' -e g -e 's/--#/newline/2' -e p -e \} file > newfile

and this:
sed -n -e H -e '${' -e g -e 's/^--#$/newline/2' -e p -e \} file > newfile

The first one changes the second line, but when i added the ^ and $ in the second one, nothing happens!

Can anyone 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

Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello, I am on a Mac and trying to clean up some monthly files with a very simple SED: sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt (from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file) then output to output.txt Even when I try... (2 Replies)
Discussion started by: verbatim
2 Replies

2. Shell Programming and Scripting

awk command for simple join command but based on 2 columns

input1 a_a a/a 10 100 a1 a_a 20 200 b1 b_b 30 300 input2 a_a a/a xxx yyy a1 a1 lll ppp b1 b_b kkk ooo output a_a a/a 10 100 xxx yyy (2 Replies)
Discussion started by: ruby_sgp
2 Replies

3. Shell Programming and Scripting

sed - simple qx

Im usind se as follows, sed 's/**** DRAFT ****/ /' a.lst > b.lst '**** DRAFT ****' in a.lst goes to' ****' in b.lst Does anyone know the right syntax? Thanks!! ---------- Post updated at 11:02 AM ---------- Previous update was at 11:00 AM ---------- ... (4 Replies)
Discussion started by: ttilsch
4 Replies

4. Shell Programming and Scripting

simple sed question

How do I remove parentheses using sed? input (192.168.1.1) output 192.168.1.1 (4 Replies)
Discussion started by: streetfighter2
4 Replies

5. Shell Programming and Scripting

Simple sed command

I have some troubles with this: insert (at the beginning of line) character "#" from line 5 to line 15 (3 Replies)
Discussion started by: aspire
3 Replies

6. Shell Programming and Scripting

simple sed

Hello Experts, I am being silly here and just need someone to point out what the silliness is I have a bunch of lines that are 12343 words here that can chage (hat:98-345) more word and numbers here I just want to pick out the numbers after "hat:" which is in every line. I have been... (8 Replies)
Discussion started by: gobi
8 Replies

7. Shell Programming and Scripting

Simple SED edit

I have output like the following: B D 20070116095820001 N D S0000579.LOG S0000582.LOG B D 20070116095750001 N D S0000574.LOG S0000576.LOG B D 20070116095734001 N D S0000570.LOG S0000573.LOG B D 20070116095705001 N D S0000569.LOG S0000569.LOG B D ... (5 Replies)
Discussion started by: rdudejr
5 Replies

8. UNIX for Dummies Questions & Answers

Simple Sed

Hello. Just trying to write this line to an empty file. CAT shows nothing was written. Any suggestions or answers? #!/bin/bash -x THIS=FIRSTLINE sed '1w '$THIS'' testfile cat testfile Thank you. (2 Replies)
Discussion started by: steveramsey
2 Replies

9. Shell Programming and Scripting

Simple sed??

I am obviously missing something here, but the following simple command is giving me problems: sed 'i\extratext' filename.txt I receive "sed: command garbled: i\extratext' " Any suggestions? Thanks. (6 Replies)
Discussion started by: here2learn
6 Replies

10. Shell Programming and Scripting

probably simple SED-command ...

Hello, I've got a problem with SED. It's my intention to shorten a file path (removing the file name) with the help of SED. Something like: tmp\folder1\folder2\blah.txt has to be transformed to tmp\folder1\folder2\. I suppose, it's on the tip of my tongue. Perhaps it's close to: sed... (2 Replies)
Discussion started by: sysadv
2 Replies
Login or Register to Ask a Question