English 中文(简体)
第一种情况替换案文
原标题:sed first occurrence text replacement
  • 时间:2012-04-25 13:31:11
  •  标签:
  • sed

我想逐字逐字删除,我尝试:

find temp -type d | while read DIRNAME
do
    sed -i  /abcd1/,/abcd2/d  ~/Desktop/$DIRNAME/.x
done

问题在于,我只想消除第一次发生这种情况,而是要消除这种情况。

sed -i  0,/abcd1/,/abcd2/d  ~/Desktop/$DIRNAME/.x

没有工作。

问题回答
sed -ie  bb; :a q; :b s/from/into/; ta  test.txt
  • bb; — branch to the b mark (skipping the quit command);
  • :a q;a mark with the quit command;
  • :b s/from/intob mark with substitution;
  • ta — jump to a mark if substitution is successful;
  • ; — command delimiter in sed.

希望会有所帮助。

一种方式:

sed -e  /3/,/6/ { /6/ ba ; d }; b ; :a ; $! { N ; ba }; s/[^
]*
// 

<>strong>EDIT,解释:

/3/,/6/         # Range between line that matches  3  and other line that matches  6 .
{
  /6/ ba        # When matches last line of the range, goto label  a .
  d             # Delete each line in the range.
}
b               # This command will be executed in lines until first match in the 
                # range. It prints the line and begin loop reading next one.
:a              # Label  a .
$!              # While not found last line ...
{ 
  N             # Append next line to current pattern space.
  ba            # Go to label  a . Enter a loop reading each line until last one.
}
s/[^
]*
//    # Remove until first  
  (content of last line of the range) and print 
                # the rest.

www.un.org/spanish/ga/president

infile:

1
2
3
4
5
6
7
1
2
3
4
5
6
7

赞成:

sed -e  /3/,/6/ { /6/ ba ; d }; b ; :a ; $! { N ; ba }; s/[^
]*
//  infile

Output (only deletes lines between 3 and 6 once):

1
2
7
1
2
3
4
5
6
7

这或许有助于你:

sed  /word1/!b;:a;/word1.*word2/{s///;tb};$!{N;ba};:b;$!{n;bb}  file

以上用词,用行各行各行各行:

sed  /block1/,$!b;x;/./{x;b};x;/block1/,/block2/{/block2/h;d}  file

EDIT:

删除<代码>第1行,但只有不止一次出现:

 sed  /word1/!b;:a;/(word1).*(word2)(.*1.*2)/{s//3/;tb};$!{N;ba};:b;$!{n;bb}  file




相关问题
how to tell sed "dot match new line"

I can t figure how to tell sed dot match new line: echo -e "one two three" | sed s/one.*two/one/m I expect to get: one three instead I get original: one two three

sed: using search pattern in output

I d like to use the search pattern in the output part of a sed command. For instance, given the following sed command: sed /HOSTNAME=/cHOSTNAME=fred /etc/sysconfig/network I d like to replace the ...

Text manipulation and removal

I have text files generated by one of my tools with structure shown below. 1 line text (space) multiple lines text (space) multiple lines text nr 2 ----------------------------------------------------...

Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract ...

Bulk Insert Code Before </body> Tag in 100 Files

I d like to insert <?php include_once( google_analytics.php ); ?> before the closing body tag of about 100 php files. Unfortunately the person who made the site didn t make a header or ...

Extract float from text line with sed?

I am issuing a sed replace on a line, trying to extract a particular floating-point value, but all that appears to be matched is the right-side of the decimal Text Line: 63.544: [GC 63.544: [DefNew: ...

热门标签