Sed: print 2 lines before a match [closed] - sed

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to print the two previous lines before matches inside a file (for any match)
How to make it?
Thank you

The script:
sed -n "1N;2N;/XXX[^\n]*$/{h;s/\n[^\n]*$//;p;g};N;D"
works as follows:
Read the first three lines into the pattern space, 1N;2N
Search for the test string XXX in the last line, and if found: save pattern space in hold space h, delete last line s, print p, and then restore saved string g
Append the next line input to pattern space, N
Delete first line from pattern space and restart cycle, D, noting that 1N;2N is no longer applicable
See also similar SED: addressing two lines before match.

If you do not insist on using sed, use
grep -B 2

Related

Perl Split Operator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
I have a large text file of records each beginning with 22-. I want to read in the file and split it up into an array with one record per element. Looks like I will lose the 22- at the beginning of each element. Is there a way to split it without losing the 22-? I suppose after the split I could add the 22- back to the beginning of each element.
I haven't coded it yet
With split, you specify the separator. 22- is not a separator, but part of the record.
You can still use split by separating on the zero-width space that's followed by 22-.
split /(?=22-)/

sed - how to create all possible variations of name combinations [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I cannot seem to find a clever way of creating all possible variations of name combinations.
Input (multiple names put together where each name is represented as a letter):
ABC (e.g. JohnPeterSarah)
pattern space: Replaces A with 1, B with 2 and C with 3 (among other -that's where you come in)
Desired Output
ABC
A2C
A23
AB3
1BC
12C
1B3
sed is not the most natural tool for this task. Try bash's brace expansion:
$ printf "%s\n" {A,1}{B,2}{C,3}
ABC
AB3
A2C
A23
1BC
1B3
12C
123
Its not clear what you are trying to do here but it sounds like you want all permutations of a string. The best way to do that is using recursion. This question was already addressed here.

Could you please tell me what this line do: sed 's/^.*-svn\([0-9]*\).*$/\1/' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Could you please tell me what this line do of sed
the expression is on the title
Thank you
Best regards
It replaces everything on the line with the number immediately following "-svn".
eg.:
blahblabhlabhlabhalbh-svn12345blahblhablhab
is replaced by:
12345
It's matching a string of [anything]-svn[numbers][anything], and replacing the line with the [numbers].
For example:
asda-svn123123bebeb
Gets replaced with
123123

What is the convention for line wrapping in command-line instructions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I need to document some very long command line instructions. What convention should I use to indicate that the line is not broken?
For example, for the command:
fab --fabfile=create_virtualenv.py --hosts=<user#remote> create_virtualenv:base=<base_folder>,name=<ve_name>,requirements=<requirements_file>,packages=<package_folder>
I was thinking:
fab --fabfile=create_virtualenv.py --hosts=<user#remote> \\
create_virtualenv:base=<base_folder>,name=<ve_name>,\\
requirements=<requirements_file>,packages=<package_folder>
but that's just me making something up based on a half-forgotten example, and I can see potential for ambiguities regarding whitespace and punctuation.
A referenced standard would be best, or consistent, well-regarded implementation.
On Windows, I would use the same character that CMD.EXE uses for line continuation - the caret ^.
fab --fabfile=create_virtualenv.py --hosts=<user#remote> ^
create_virtualenv:base=<base_folder>,name=<ve_name>,^
requirements=<requirements_file>,packages=<package_folder>
See Long commands split over multiple lines in Windows Vista batch (.bat) file for more info. Be sure to read the first three answers, as they each have useful information.
On 'nix, I would use a single \ for the same reason. A double \\ would imply a backslash literal, so that is not good. But a single \ at the end implies line continuation.
fab --fabfile=create_virtualenv.py --hosts=<user#remote> \
create_virtualenv:base=<base_folder>,name=<ve_name>,\
requirements=<requirements_file>,packages=<package_folder>

Comments are getting executed when I run the Perl Script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Some of my perl statements are executed even after commenting. I tried all the delimiter #, /*..*/, //
Does anyone face the same issue or can anyone help me how to solve this issue?
Your question appears to be "How do I create a comment in Perl?"
perlsyn says thusly:
Text from a "#" character until the end of the line is a comment, and is ignored. Exceptions include "#" inside a string or regular expression.
For example,
print "apple\n"; # Keeps the doctor away.
Keep in mind that comments can only be used where whitespace is expected. For example, the following does not contain any comments since the # is part of the string literal.
print "apple # Keeps the doctor away.
orange
";