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.
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 ...?
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.)
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)