How to use sed modify specific lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use sed modify specific lines
# 1  
Old 05-18-2008
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  
Old 05-18-2008
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 06:46 AM.. Reason: Maybe prefer awk or Perl after all ...?
# 3  
Old 05-18-2008
Thanks, it works.

I wonder how to use awk to do this.
# 4  
Old 05-18-2008
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  
Old 05-18-2008
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  
Old 05-18-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Finding lines of specific size in files using sed

i am using sed to detect any lines that are not exactly 21. the following gives me the lines that ARE exactly 21. i want the opposite , i want the two lines that are not size 21 (shown in bold) type a.a 000008050110010201NNN 000008060810010201NNN 21212000008070110010201NNN... (5 Replies)
Discussion started by: boncuk
5 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

5. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

6. Shell Programming and Scripting

Sed - merge lines bw 2 specific characters

Hi, I have a bash script and I am looking for a command that will merge specific lines together. Sample Data: registration time = 1300890272 Id = 1 setd = 0 tagunt = 26 tagId=6, length=8, value= tagId=9, length=5, value= tagId=7, length=2, value= tagId=16, length=2, value= tagId=32,... (8 Replies)
Discussion started by: Winsarc
8 Replies

7. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

8. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies

9. Shell Programming and Scripting

Sed one-liner to print specific lines?

I need to print specific lines from a file, say 2-5, 8, 12-15, 17, 19, 21-27. How do I achieve this? (2 Replies)
Discussion started by: Ilja
2 Replies

10. Shell Programming and Scripting

SED: joining specific lines

I have a .txt file containing lines like: ZAN ZAN name = AI_EVENT desc (further text) ZAN ZAN name = AI_EVENT desc ... ZAN ZAN1 name = AI_EVENT desc ... ZAN ZAN1 name = AI_EVENT desc ... WUR WUR name = AI_EVENT desc ... WUR WUR name = AI_EVENT desc ... VEN VEN name = AI_EVENT desc ... VEN... (2 Replies)
Discussion started by: Gnollish
2 Replies
Login or Register to Ask a Question