Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Substitution in first occurrence with sed Post 302969208 by Don Cragun on Saturday 19th of March 2016 05:31:25 PM
Old 03-19-2016
Quote:
Originally Posted by bakunin
With the sample input: yes. But if the "string1" happens to be not on the starting line of the input file there would be a difference. Your code would print everything from first line to a line with "string2".

This is why I'd prefer Scrutinizers solution even if your code is shorter.

bakunin
Based on the output shown, I tend to agree. But, in the code Xterra was using:
Code:
sed '0,/string1/,/string2/!d' infile

the 0,/string1/ might be trying to print the 1st line of the file up to and including the first line containing string1. With the confusing description and sample input provided, I don't know whether the desired output from an input file containing:
Code:
before
string1
during
string2
after

would be:
Code:
before
string1
during

which my suggestion would produce, or:
Code:
]string1
during

which Scrutinizer's suggestion would produce.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

2. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

3. Shell Programming and Scripting

Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems. Ex. How are you?</EndTag><Begin>It is fine.</Begin><New> Just about I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>) I tried the following sed... (2 Replies)
Discussion started by: quixoticking11
2 Replies

4. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

5. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

7. Shell Programming and Scripting

Sed diffrent replace by occurrence

I couldn't find the answer anywhere, so I hope you could help me. I need to change something like the following: something/bla/aaaa anything/bbb to: something --bla ----aaaa anything --bbb How do I do this? Is it possible with sed? I tried various patterns, but don't know how to... (5 Replies)
Discussion started by: Patwan
5 Replies

8. Shell Programming and Scripting

Modifying sed to only change last occurrence.

I'm using sed to switch integers (one or more digits) to the other side of the ':' colon. For example: "47593:23421" would then be "23421:47593". The way it functions right now, it is messing my settings file to use with gnuplot. The current command is: sed 's/\(*\):\(*\)/\2:\1/' out3 >... (3 Replies)
Discussion started by: D2K
3 Replies

9. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

10. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies
sed(1)							      General Commands Manual							    sed(1)

NAME
sed - stream text editor SYNOPSIS
script [file]... script]... script_file]... [file]... DESCRIPTION
copies the named text files (standard input default) to the standard output, edited according to a script containing up to 100 commands. Only complete input lines are processed. Any input text at the end of a file that is not terminated by a new-line character is ignored. Options recognizes the following options: Take script from file script_file. Edit according to script. If there is just one option and no options, the flag can be omitted. Suppress the default output. interprets all and arguments in the order given. Use caution, if mixing and options, to avoid unpredictable or incorrect results. Command Scripts A script consists of editor commands, one per line, of the following form: [address address]] function [arguments] In normal operation, cyclically copies a line of input into a pattern space (unless there is something left after a command), applies in sequence all commands whose addresses select that pattern space, and, at the end of the script, copies the pattern space to the standard output (except under and deletes the pattern space. Some of the commands use a hold space to save all or part of the pattern space for subsequent retrieval. Command Addresses An address is either a decimal number that counts input lines cumulatively across files, a which addresses the last line of input, or a context address; that is, a in the style of ed(1) modified thus: o In a context address, the construction where ? is any character, is identical to Note that in the context address the second stands for itself, so that the regular expression is o The escape sequence matches a new-line character embedded in the pattern space. o A period matches any character except the terminal new-line of the pattern space. o A command line with no addresses selects every pattern space. o A command line with one address selects each pattern space that matches the address. o A command line with two addresses selects the inclusive range from the first pattern space that matches the first address through the next pattern space that matches the second (if the second address is a number less than or equal to the line number first selected, only one line is selected). Thereafter the process is repeated, looking again for the first address. supports Basic Regular Expression syntax (see regexp(5)). Editing commands can also be applied to only non-selected pattern spaces by use of the negation function (described below). Command Functions In the following list of functions, the maximum number of permissible addresses for each function is indicated in parentheses. Other func- tion elements are interpreted as follows: text One or more lines, all but the last of which end with to hide the new-line. Backslashes in text are treated like back- slashes in the replacement string of an command, and can be used to protect initial blanks and tabs against the strip- ping that is done on every script line. rfile Must terminate the command line, and must be preceded by exactly one blank. wfile Must terminate the command line, and must be preceded by exactly one blank. Each wfile is created before processing begins. There can be at most 10 distinct wfile arguments. recognizes the following functions: text Append. Place text on the output before reading next input line. Branch to the command bearing label. If no label is specified, branch to the end of the script. text Change. Delete the pattern space. With 0 or 1 address or at the end of a 2-address range, place text on the output. Start the next cycle. Delete pattern space and start the next cycle. Delete initial segment of pattern space through first new-line and start the next cycle. Replace contents of the pattern space with contents of the hold space. Append contents of hold space to the pattern space. Replace contents of the hold space with contents of the pattern space. Append the contents of the pattern space to the hold space. text Insert. Place text on the standard output. List the pattern space on the standard output in an unambiguous form. Non-printing characters are spelled in three-digit octal number format (with a preceding backslash), and long lines are folded. Copy the pattern space to the standard output if the default output has not been suppressed (by the option on the command line or the command in the script file). Replace the pattern space with the next line of input. Append the next line of input to the pattern space with an embedded new-line. (The current line number changes.) Print. Copy the pattern space to the standard output. Copy the initial segment of the pattern space through the first new-line to the standard output. Quit. Branch to the end of the script. Do not start a new cycle. Read contents of rfile and place on output before reading the next input line. Substitute replacement string for instances of regular expression in the pattern space. Any character can be used instead of For a fuller description see ed(1). flags is zero or more of: n n=1-2048 Substitute for just the nth occurrence of regular expression in the pattern space. Global. Substitute for all non-overlapping instances of regular expression rather than just the first one. Print the pattern space if a replacement was made and the default output has been suppressed (by the option on the command line or the command in the script file). Under the environment (see standards(5)), the pattern space is printed twice, if the option is not specified on the command line. Write. Append the pattern space to wfile if a replacement was made. Test. Branch to the command bearing the label if any substitutions have been made since the most recent reading of an input line or execution of a If label is empty, branch to the end of the script. Write. Append the pattern space to wfile. Exchange the contents of the pattern and hold spaces. Transform. Replace all occurrences of characters in string1 with the corresponding character in string2. The lengths of string1 and string2 must be equal. Any character other than backslash or newline can be used instead of slash to delimit the two strings. If a backslash character is immediately followed by another backslash character in string1 or string2, the two backslash char- acters will be handled as a single literal backslash character. The delimiter itself can be used as a literal character in string1 and string2 if it is preceded by a backslash. The only exception is character "n" as the delimiter, because backslash-n, " ", will be interpreted as a newline. The meaning of a backslash is undefined if it is followed by any character that is not "n", a backslash, or the delimiter char- acter. Don't. Apply the function (or group, if function is only to lines selected by the address or addresses. This command does nothing; it bears a label for and commands to branch to. Place the current line number on the standard output as a line. Execute the following commands through a matching only when the pattern space is selected. The syntax is: (0) An empty command is ignored. If a appears as the first character on the first line of a script file, that entire line is treated as a comment with one exception: If the character after the is an the default output is suppressed. The rest of the line after is also ignored. A script file must contain at least one non-comment line. EXTERNAL INFLUENCES
For information about the UNIX Standard environment, see standards(5). Environment Variables provides a default value for the internationalization variables that are unset or null. If is unset or null, the default value of "C" (see lang(5)) is used. If any of the internationalization variables contains an invalid setting, will behave as if all internationalization variables are set to "C". See environ(5). If set to a non-empty string value, overrides the values of all the other internationalization variables. determines the interpretation of text as single and/or multi-byte characters, the classification of characters as printable, and the char- acters matched by character class expressions in regular expressions. determines the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informa- tive messages written to standard output. determines the location of message catalogues for the processing of International Code Set Support Single- and multi-byte character code sets are supported. EXAMPLES
Make a simple substitution in a file from the command line or from a shell script, changing to Same as above but use shell or environment variables and in search and replacement strings: or Multiple substitutions in a single command: or WARNINGS
limits command scripts to a total of not more than 100 commands. The hold space is limited to 8192 characters. processes only text files. See the glossary for a definition of text files and their limitations. AUTHOR
was developed by OSF and HP. SEE ALSO
awk(1), ed(1), grep(1), environ(5), lang(5), regexp(5), standards(5). tutorial in the STANDARDS CONFORMANCE
sed(1)
All times are GMT -4. The time now is 09:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy