Skip to main content

New answers tagged

0 votes

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

Using any awk with a blank line at the end of the output: $ awk -v RS= -F'\n' -v ORS='\n\n' 'NF != 2' file aaa aaaaaa bb bbb cc cccc ccccc cccc fff ff ggg gggg gggg hhh hhh hh hhhh Using any awk ...
Ed Morton's user avatar
  • 32.5k
1 vote

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

With GNU awk: awk 'BEGIN{FS=RS; RS=""} NF==3{print; print ""}' file Output: aaa aaaaaa bb bbb cc cccc ccccc cccc fff ff ggg gggg gggg hhh hhh hh hhhh Output contains one ...
Cyrus's user avatar
  • 12.4k
-1 votes
Accepted

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

You can use perl for this (along with several other possibilities) perl -n -00 -e 'chomp; print "$_\n\n" unless y/\n// == 1' datafile Or expanded out somewhat, perl -n -00 -e ' # -n -00 ...
Chris Davies's user avatar
0 votes

Use sed to insert a character in an xml file

Assuming a well-formed XML document, like this: <?xml version="1.0"?> <root xmlns:type="uri://something/type"> <type:color>000000</type:color> <type:...
Kusalananda's user avatar
  • 339k
2 votes
Accepted

Use sed to insert a character in an xml file

This looks to be pretty simple: cat - << EOF | <type:color>000000</type:color> <type:color>FFFFFF</type:color> EOF sed 's/<type:color>/<type:color>#/' Or ...
user1683793's user avatar
1 vote

Sed command listing all lines between pattern including the final line

Is there only one line in the section? Then grep could do it, e.g.: grep -FA1 '[selinux=disabled]' infile Output: [selinux=disabled] insecure = enabled
Thor's user avatar
  • 17.3k
0 votes

Replace comma with newline following with just a command and space

Using Raku (formerly known as Perl_6) Slurping the file in all-at-once: ~$ raku -e 'slurp.subst(:global, / \, \n /, ",").put;' file OR: Reading linewise and combining every three lines: ~$ ...
jubilatious1's user avatar
  • 3,395
0 votes

How can I comment out or in a section in source code with sed or awk

This popped up today. The solutions proposed seem overly complicated: /# BEGIN SOMENAME/ { prefix="# "; } /# END SOMENAME/ { prefix=""; } { print prefix $0; } (although the ...
symcbean's user avatar
  • 5,839
0 votes

Replace comma with newline following with just a command and space

Looks like you are simply trying to merge every three lines. If that is the case the paste command is probably the easiest option: $ paste -d '' - - - < infile blue-image1.jpg,blue-image2.jpg,blue-...
user167612's user avatar
  • 1,700
0 votes

How to decode base64 text in ldif file in Linux?

The sed command piped into awk in elysch’s answer didn't work for me on some of my data, so I changed that one line to be the following perl: perl -pe 'use MIME::Base64; s/(.*:)(: )([[:graph:]]+)/$1 . ...
Frank Tate's user avatar
6 votes

What does '\($*\)' mean in sed regular expression in a makefile?

It's important to keep in mind that normally regular expressions given on the command line to sed are subject to two rounds of parsing: once by sh (which is why you put in in ' quotes), and again by ...
Martin Kealey's user avatar
10 votes
Accepted

What does '\($*\)' mean in sed regular expression in a makefile?

The $ used here is the dollar sign of the $* automatic Make variable, not something that denotes the end of line. You have a similar example of this in the replacement part of the sed substitution, ...
Kusalananda's user avatar
  • 339k
1 vote

Need to make multiple multiline replacements in file

Assuming you want to ONLY change blocks of text that match EXACTLY the block shown in your question and that your input always looks exactly as shown in your question then using any POSIX awk you ...
Ed Morton's user avatar
  • 32.5k
1 vote

Need to make multiple multiline replacements in file

After adding enclosing tag r to your XML: xmlstarlet ed -u '//filename' -v 'FILE_PATH/File Name' \ -u '//transname' -v '' file.xml <?xml version="1.0"?> <r> <type>...
Gilles Quénot's user avatar
2 votes

Sed command listing all lines between pattern including the final line

Remove all lines that do not match the bracketed heading with /RE/!d. If a line matches the heading, output it and read the next line with n. The extra line that is read will be outputted by the ...
Kusalananda's user avatar
  • 339k
0 votes

Sed command listing all lines between pattern including the final line

If just one line follow the one you need: sed '/[[]selinux=disabled[]]/{n;q};d' test If more then one lines follow [selinux=disabled] section: sed '/[[]selinux=disabled[]]/{:1 n;/^[[]/!b1};d' test
DanieleGrassini's user avatar
3 votes
Accepted

Sed command listing all lines between pattern including the final line

You can search for the first line and then scope a series a commands from there; basically print it, read in the next line, print it, quit. % sed -n '/\[selinux=disabled\]/ {p;n;p;q}' y [selinux=...
Stephen Harris's user avatar
1 vote

Cut and replace Nth character on every row

Using GNU awk: The following command is used taking cue from the answer: $ awk '{print gensub(/(^.{18})..(.*)/, "\\1x\\2", "g")}' file If input hasn't TAB as a character in input, ...
Prabhjot Singh's user avatar
0 votes

Convert timestamp to new format using SED

Assuming you want to leave the rest of the line unchanged but remove all cases of a [ at the beginning of a line followed by three sets of numbers, separated by a slash and everything else until the ...
terdon's user avatar
  • 245k
0 votes

Convert timestamp to new format using SED

A script like the following does what you want, assuming there's nothing else on the line. # Remove square brackets and convert spaces to slashes dateonly=$(echo "[2024/06/10 10:03 PM]" | tr ...
Peter bill's user avatar
4 votes

Convert timestamp to new format using SED

Assuming a sed implementation with support for -E for extended regexps (only standard since the 2024 edition of POSIX). d='[0123456789]' sed -E "s|\[($d{4})/($d$d)/($d$d) $d$d:$d$d [AP]M\]|\1\2\3....
Stéphane Chazelas's user avatar
3 votes

Cut and replace Nth character on every row

As sed is neither in the title or tagged, and the input is predictable, I would offer: awk '{ print substr ($0, 1, 18) "x" substr ($0, 21); }' Has the advantage of being explicit, and the ...
Paul_Pedant's user avatar
  • 9,074
5 votes

Cut and replace Nth character on every row

If you need to replace the 19th and 20th character of each line with x if and only if they are , and space respectively, you can do: sed 's/^\(.\{18\}\), /\1x/' With most sed implementations (all ...
Stéphane Chazelas's user avatar
10 votes
Accepted

Cut and replace Nth character on every row

The simplest way given your example input is to just replace the first comma-and-space with an x (here, file has your example): $ sed 's/, /x/' file 775448167763476486x783834143007506433, 35972, ...
terdon's user avatar
  • 245k
0 votes

regular single column data to multiple rows

Using GNU awk for multi-char RS and RT: $ awk -v RS='KKK-SNMOdb: NONE\n' -F'\n' -v OFS=',' '{ORS=RT; $1=$1; print}' file KKK-SNMImsi: 444444,KKK-SNMUserProfileId: KKK-SNMDefaultAutomaticProfile,KKK-...
Ed Morton's user avatar
  • 32.5k
4 votes

How can UserA programmatically replace a text string within a file they own that's located in /etc?

I did not mention this case in the Perl Cookbook because it is so rare, but because your replacement string is the same length exactly as your original string, it actually is possible to update the ...
tchrist's user avatar
  • 473

Top 50 recent answers are included