sed - Print Edited Line Along With Original Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - Print Edited Line Along With Original Line
# 1  
Old 10-30-2013
sed - Print Edited Line Along With Original Line

Hi,

I have an input file like this

Code:
line1

line2

line3 hello unix how are you

This is what I am expecting my output to be

Code:
line1

line2

#line3 hello unix how are you

line3 hello

Basically, I would like to comment out the 3rd line and in addition print another line. It would be good to know if I can tell the script what my fourth line should be.
# 2  
Old 10-30-2013
Code:
sed -e 'h;s/^\(line3 .*\)/#\1/p;x;s/^line3 \([^ ]*\).*/\nline3 \1/'

Because of the '\n', may require GNU sed or a contemporary sed.
This User Gave Thanks to cjcox For This Post:
# 3  
Old 10-30-2013
Quote:
Originally Posted by cjcox
Code:
sed -e 'h;s/^\(line3 .*\)/#\1/p;x;s/^line3 \([^ ]*\).*/\nline3 \1/'

Because of the '\n', may require GNU sed or a contemporary sed.
Thanks cjcox.

But, there are files where I have to print

Code:
line3 how or line3 you

# 4  
Old 10-30-2013
Try something like:
Code:
awk '$0~s{$0="#" $0 ORS r}1' s=line3 r="line3 how or line3 you" file

or
Code:
awk '$1==s{$0="#" $0 ORS r}1' s=line3 r="line3 how or line3 you" file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

2. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

sed print first line before regexp and all lines after

Hi All I'm trying to extract the line just above a regexp and all lines after this. I'm currently doing this in two steps sed -n -e "/^+---/{g;p;}" -e h oldfile.txt > modified.txt sed -e "1,/^+---/d" -e "/^$/d" oldfile.txt >>modified.txt Sample sometext will be here sometext will be... (3 Replies)
Discussion started by: Celvin VK
3 Replies

4. Shell Programming and Scripting

How to delete a duplicate line and original with sed.

I am completely new to shell scripting but have been assigned the task of creating several batch files to manipulate data. My final task requires me to find lines that have duplicates present then delete not only the duplicate but the original as well. The script will be used in a windows... (9 Replies)
Discussion started by: chino_1
9 Replies

5. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

6. Shell Programming and Scripting

Print portion line in SED

Hi, This is more a theoretical question, because I usually solved that with perl or even java, but I would like to know if it exists an easy way to do it with SED. Using regular expresions it's very easy to select an portion line. Does it exist an easy way for printing those portions in SED?... (1 Reply)
Discussion started by: islegmar
1 Replies

7. UNIX for Dummies Questions & Answers

use variable for sed print line

I'm using the sed command to pull a line from a file and place it into another file: sed -n '1p' students >tmp this pulls the first line, I want to change the 1 to a variable which will be a counter to go through the rest of the lines but can't get it. I've tried: sed -n '$cp students... (3 Replies)
Discussion started by: atchleykl
3 Replies

8. Shell Programming and Scripting

sed - print old line and new changed line

hello, i have a file "TEST" and want to change the digit(s) after "=" . but i also want to print the old entry with a comment (for information). i want to use "sed", is it possible ? file: TEST = 10 # comment default value: 0 sed , with "p" , i can print the old entry, but i want to... (2 Replies)
Discussion started by: bora99
2 Replies

9. Shell Programming and Scripting

sed: print out to first blank line

I am trying to use wget and sed to extract a text based weather forecast for the Greater Victoria area only, this is one paragraph of many in a web page. I have tried /^$/ but that does not seem to work. So far I get mostly what I want with this: wget -qO -... (2 Replies)
Discussion started by: lagagnon
2 Replies

10. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies
Login or Register to Ask a Question