If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > using sed in selected lines

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-25-08, 10:46
nebilben nebilben is offline
Registered User
 
Join Date: Mar 2008
Posts: 7
using sed in selected lines

Hello everyone,
Am writin a script with awk aiming to substitute a message in selected lines
i dont know how to insert sed command in the if condition to replace only the selected lines

awk /^VDD/'{

getline;getline;v=split($2,tab,"1V8_2V5_3V3") ;

if ($1=="|" && v!=1) {getline;getline; sed 's/ERROR/warning/g' file> tmp }


}' file
Reply With Quote
  #2 (permalink)  
Old 03-25-08, 16:12
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,452
sed is not an awk built-in function; it's a separate utility. Try using sub().
Reply With Quote
  #3 (permalink)  
Old 03-26-08, 04:44
nebilben nebilben is offline
Registered User
 
Join Date: Mar 2008
Posts: 7
Thank U for repliyin
I've tried to use sub() but it doesent work
i tried this
awk /^VDD/'{

getline;getline;v=split($2,tab,"1V8") ;

if ($1=="|" && v!=1)
{
getline;getline; str=$0;
sub (/ERROR/,"Warning",str); print str
}
}'

(I want to use sub to make a modification only in the selected line)
Reply With Quote
  #4 (permalink)  
Old 03-26-08, 10:36
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,452
Have you tried reading the awk manual? There are some examples showing how to properly use the sub() function.
Reply With Quote
  #5 (permalink)  
Old 03-26-08, 11:52
nebilben nebilben is offline
Registered User
 
Join Date: Mar 2008
Posts: 7
Thank U i have used the gsub in a gawk command and its OK
but now I am trying to save the modif of selected lines in the source file not in a new file (I've tried to use print "$0" >> filename but it puts the new lines in the end of the file)
gawk /^VDD/'{

getline;getline;v=split($2,tab,"1V8_") ;if ($1=="|" && v!=1)

{
getline;getline;str=$0;gsub(/ERROR/,"warning",str) ;
print str >> "checkSupply"
}
}'
Reply With Quote
  #6 (permalink)  
Old 03-26-08, 13:25
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,452
You cannot do it with awk. You'll have to write to a new file, then rename. If you think you need to edit the file in place you should use something like ed or vi.
Reply With Quote
  #7 (permalink)  
Old 03-26-08, 13:42
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Rather than giving us your code, why not just give a quick description of what you want and a few lines of test data and what you would expect as output. It sounds like something than can be done by sed alone but I can't work out what you're trying to do by looking at your code.
Reply With Quote
  #8 (permalink)  
Old 03-27-08, 02:37
nebilben nebilben is offline
Registered User
 
Join Date: Mar 2008
Posts: 7
In fact I have a file which contains ERROR messages.Some of these messages are wrong So I must select these wrong messages and make a modif only on these lines. Now with my code I can select the lines but I cant make the modif on the source file . The file must stay the same Only selected lines have to be modified
Reply With Quote
  #9 (permalink)  
Old 03-27-08, 04:46
nebilben nebilben is offline
Registered User
 
Join Date: Mar 2008
Posts: 7
Problem solved
Thank U 4 all
Reply With Quote
  #10 (permalink)  
Old 03-27-08, 04:48
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You would run something like the following. Seeing as you don't want to give a proper example I've made up my own text. You'll need a sed command for each message you want to change. I'll assume you know about the pipe character | etc.

Code:
cat OldErrorFile | sed 's/BadMsg1/GoodMsg1/g' | sed 's/BadMsg2/GoodMsg2/g' > NewErrorFile
mv NewErrorFile OldErrorFile
Mike
Reply With Quote
  #11 (permalink)  
Old 03-27-08, 06:27
nebilben nebilben is offline
Registered User
 
Join Date: Mar 2008
Posts: 7
Thanks for the idea I'll try to use it maybe to improve my code
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On