![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
Code:
sed -e '/^"TEST/{p;N;s/.*\n[0-9]*/0/;}' filename
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 ...? |
|
|||
|
Code:
awk '/^"TEST/ { t=1 ; print; next }
t==1 { $1 = "0"; t=0; }1' filename
|
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |
| Tags |
| regex, regular expressions |
| Thread Tools | |
| Display Modes | |
|
|