The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing specific lines catgovind Shell Programming and Scripting 3 04-30-2008 02:40 AM
How to read a specific section and modify within kn.naresh Shell Programming and Scripting 2 04-17-2008 09:30 PM
Printing lines with specific awk NF jehrome_rando Shell Programming and Scripting 1 03-13-2007 12:23 AM
viewing specific lines dakid UNIX for Dummies Questions & Answers 3 05-25-2006 12:25 AM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-22-2005 09:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: Apr 2008
Posts: 16
Stumble this Post!
How to use sed modify specific lines

Could anybody tell me how I can use sed to modify lines following specific lines? the file is as following:

"TEST/SI1573.lab"
3670 8920 h#
8920 9530 hh
9530 10694 ih
.
"TEST/DR1/FAKS0/SI2203.lab"
9730 9580 h#
9580 9840 dh
9840 10652 ix
10652 11997 r
........

I want to modify the first fields to 0 of the lines which follows the lines started with "TEST. The file after operation should be:
"TEST/SI1573.lab"
0 8920 h#
8920 9530 hh
9530 10694 ih
.
"TEST/DR1/FAKS0/SI2203.lab"
0 9580 h#
9580 9840 dh
9840 10652 ix
10652 11997 r
........
could anybody tell my how to do that? Thanks in advance.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,599
Stumble this Post!
Code:
sed -e '/^"TEST/{p;N;s/.*\n[0-9]*/0/;}' filename
There are many different versions of sed, so yours might not understand exactly the same dialect as mine.

This looks for '"TEST' (with an opening double quote) at beginning of line. If found, it prints that line (p), and appends the next line to the pattern space (N). This causes the pattern space to contain two lines; the TEST line and the following line, separated by a newline. Then it replaces (s///) the first line in the pattern space, the newline, and any numbers just after the newline with a zero. At that point, we are done; whatever is left in the pattern space will be printed as usual.

sed syntax is very terse; if you don't have a specific reason to use sed for this, perhaps an equivalent awk or Perl script would be more maintainable (especially if you are not very familiar with sed).

Last edited by era; 05-18-2008 at 02:46 AM. Reason: Maybe prefer awk or Perl after all ...?
Reply With Quote
  #3 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: Apr 2008
Posts: 16
Stumble this Post!
Thanks, it works.

I wonder how to use awk to do this.
Reply With Quote
  #4 (permalink)  
Old 05-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,599
Stumble this Post!
Code:
awk '/^"TEST/ { t=1 ; print; next }
t==1 { $1 = "0"; t=0; }1' filename
This causes the variable t to be set to 1 on the following line after the TEST line. If this is the case, replace the first field with a zero, and set t back to zero. The final 1 causes any line which reaches that point in the script to be printed. (It's a shorthand; the default action of awk is to print if the condition is true; and 1 as a condition is always true.)
Reply With Quote
  #5 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: Apr 2008
Posts: 16
Stumble this Post!
Thanks, era.

Do you think you can recommend me some book or websit? I would like to learn more about script. I alwasy encounter some problems, and could not figure out by myself.
Reply With Quote
  #6 (permalink)  
Old 05-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,599
Stumble this Post!
One of my personal favorites are Kernighan & Pike's The Unix Programming Environment but it's very old, so some of the peripheral details have changed quite a lot since then. Another perennial is Friedl's Mastering Regular Expressions, although you might struggle with it at first if you are not familiar with the tools it covers.

The O'Reilly sed & awk book is a good introduction to sed and awk, although there are a lot of on-line tutorials which are probably just as good.

The FAQ section has more links; I'm new to Unix. Which books should I read? - The UNIX Forums
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0