The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 05:40 AM
How to read a specific section and modify within kn.naresh Shell Programming and Scripting 2 04-18-2008 12:30 AM
Printing lines with specific awk NF jehrome_rando Shell Programming and Scripting 1 03-13-2007 04:23 AM
viewing specific lines dakid UNIX for Dummies Questions & Answers 3 05-25-2006 03:25 AM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-23-2005 12:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-18-2008
Jenny.palmy Jenny.palmy is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 16
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.
  #2 (permalink)  
Old 05-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 05:46 AM.. Reason: Maybe prefer awk or Perl after all ...?
  #3 (permalink)  
Old 05-18-2008
Jenny.palmy Jenny.palmy is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 16
Thanks, it works.

I wonder how to use awk to do this.
  #4 (permalink)  
Old 05-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.)
  #5 (permalink)  
Old 05-18-2008
Jenny.palmy Jenny.palmy is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 16
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.
  #6 (permalink)  
Old 05-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0