sed - print old line and new changed line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - print old line and new changed line
# 1  
Old 01-27-2009
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 change it with "#"

sed -e "/^\([ \t]*TEST[ \t]*=[ \t]*\)/ {
p
s/^\([ \t]*TEST[ \t]*=[ \t]*\)\([0-9]*\)\([ \t]*\)/\199\3/p
}" TEST

the new output should be:
#TEST = 10 # comment default value: 0
TEST = 99 # comment default value: 0

regards
# 2  
Old 01-27-2009
In case no sed solution is presented:
Code:
awk '/^TEST[ =]/ { print "#" $0; sub("[0-9]+",99); } { print; }'

With gawk/nawk/mawk you can pass in variables. For gawk:
Code:
gawk -v tomatch=TEST -v newval=99  \
'    /^[a-zA-Z]+[ =]/ { 
            if (match($0,"^" tomatch "[ =]")) { print "#" $0; sub("[0-9]+",newval); } 
     } 
     { print; }
'

# 3  
Old 01-28-2009
hello,

thank's for your answer, i use "akw" because the main plattform is hpux and "gawk" is a an optional software product. i extend a litte bit your "awk" and this is now my solution:
Code:
new_value=99
awk -v newval=${new_value} ' BEGIN { FS = "[ \t]*|[ \t]+" }
 $0 ~ /^[ \t]*TEST[ \t]*/ {
  print "# Overriding: " $0; sub("[0-9]+",newval); }
  { print; }' TEST

greetings,tom

Last edited by otheus; 01-28-2009 at 05:45 AM.. Reason: added [code] tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Print Edited Line Along With Original Line

Hi, I have an input file like this line1 line2 line3 hello unix how are you This is what I am expecting my output to be line1 line2 #line3 hello unix how are you line3 hello (3 Replies)
Discussion started by: jacobs.smith
3 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

awk help about show all the line beside the changed line

awk '/abc/{print $2}' This will show the line contains abc and only show the "two filed" But I want to the line contains "abc" will only show $2, other line still show. and I want to know awk's way about not only show the line besides changed line Thanks (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. Shell Programming and Scripting

print - for printing whole line, but delimeters are changed to spaces

Hi consider the source file R|field1|field2 using the print statement in awk prints it as R field1 field2 Is there an easier way to print the whole line in its original format (i.e. with | delimiters) than doing print $1"|"$2"|"$3 ?? Thanks Storms (1 Reply)
Discussion started by: Storms
1 Replies

6. 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

7. 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

8. 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

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

How to print empty line when the a field is changed

Hi all, I have this input file .. BSS01 107 Swafieh 11/06/2008 12:06:57 BSS01 111 Ramada_Hotel 12/06/2008 11:37:20 BSS01 147 Kalha_Rep 11/06/2008 19:13:39 BSS01 147 Kalha_Rep ... (9 Replies)
Discussion started by: yahyaaa
9 Replies
Login or Register to Ask a Question