# Tag Info

17

With POSIX paste: :|paste -d ' ||| ' fileA - - - - fileB paste will concatenate corresponding lines of all input files. Here we have six files, fileA, four dummy files from standard in -, and fileB. The list of delimiters include a space, three pipe and a space in that order will be used by paste circularly. For the first line of six files, fileA will ...

14

a\{0,2\} will match the empty string at the start of the line (actually, any empty string, but g wasn't specified): $echo "baaab" | sed 's/a\{0,2\}/y/' ybaaab Since GNU sed does matching from left to right, and a global replacement wasn't specified, only the start of the line matched. If you'd used g:$ echo "baaab" | sed 's/a\{0,2\}/y/g' ybyyby The ...

7

Well, this doesn't use sed, awk, or grep, but you can do it pretty easily in bash. The command is: (while IFS= read -r a <&3 && IFS= read -r b <&4; do echo "$a |||$b"; done) 3<fileA 4<fileB The problem with paste is that the delimiter is a single character. You could also insert a single character and the use sed to ...

6

Yes, it's greedy. In POSIX compliant system, not only sed but also all tools which use Basic Regular Expression, the matched pattern will always be greedy: The search for a matching sequence starts at the beginning of a string and stops when the first sequence matching the expression is found, where "first" is defined to mean "begins earliest in the ...

6

With sed: sed 's/^Question Nr\..*/\\item/; s/^$$[A-Z] .*$$/\n\1/' file The first s/// replaces Question Nr. with \item similar to the sed command in your question. The second one replaces line that start with a capital letter from A to Z, but only one followed by a space. This whole line is replaced with itself \1 repending a newline \n. The output: ...

5

sed -i "/string1.*string2\|string2.*string1/d" input.txt This will delete any line where string1 appears before string2, OR string2 appears before string1. Both strings have to be on the line, in either order, for the line to be deleted.

5

Here's an ed script: ed <<\! e file.txt /LinearRec(1, FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) -1,+1d w newfile.txt ! It writes the output to file newfile.txt. The "global" repeated version of this is ed <<\! e file.txt g/LinearRec(1, FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)/-1,+1d w newfile.txt !

5

If it doesn't need to be sed, Perl's "paragraph mode" is perfect for this. From man perlrun: -0[octal/hexadecimal] specifies the input record separator ($/) as an octal or hexadecimal number. [...] The special value 00 will cause Perl to slurp files in paragraph mode. [...] So, using -00 tells perl to define "lines" ... 5 Another way with tr+sed: tr -s \\n <infile | sed '$!G;s/Question Nr.*/\\item/' tr squeezes all newlines and then sed appends hold space content (empty newline) to each line except the last one, replacing Question Nr.* with \item. With this method you won't be able to edit the file in-place. I chose tr here as it's faster then sed's regex (even if it's ...

5

sed -e'/./!d;$!G;/^Q/c\' -e'\\item' <in >out That will delete every blank line in input, Get a blank line out of hold space and append it to all non-blank lines which are ! not the$ last, and change any pattern space ^beginning with the character Q to the one-line fixed-string \item on output. When run on your example input, the output is: \item ...

5

An awk (GNU) version awk '{printf ("%s ||| ", $0); getline < "fileB"; print$0 }' fileA With the getline command in awk, you can set $0 (all variables for columns) from next input record, if getline < "filename" you set the next$0 from the specified file. getline < "file" Set $0 from next record of file; set NF. Why your attempt ... 5 If you simply want to treat a string as a literal in sed there's already an answer for that: escaped_testx="$(sed -e 's/[\/&]/\\&/g' <<< "$TEST"; echo x)" escaped_test="${escaped_testx%x}" The extra x is to be able to handle trailing newlines, which would otherwise be removed by the command substitution.

4

With perl: perl -0777 -pe 's/\Q\{{[}\E.*?\Q{]}\}\E//gs' Note that the whole input is loaded in memory before being processed. \Qsomething\E is for something to be treated as a literal string and not a regular expression. To modify a regular file in-place, add the -i option: perl -0777 -i -pe 's/\Q\{{[}\E.*?\Q{]}\}\E//gs' file.txt With GNU awk or ...

4

To add the file name, with awk: awk -v OFS=, 'NR==1{print "file", $0};FNR==2{print FILENAME,$0}' file1 file2 ... fileN >outfile which prints the updated header if it's the first line of input (NR==1) or (||) if it's the second line of each file(FNR==2) it prints the FILENAME and the line ($0). Initial answer before your edit. You could do that with ... 4 With sed alone: sed -i '/^* !^FROM_MAILER$/d' file To remove the whole line containing the exact string * !^FROM_MAILER, with nothing before and after that string. The d command deletes the line. Edit: If you want to do the replace in all files recusively, use the following: find /path -type f -exec sed -i '/^* !^FROM_MAILER$/d' {} + 4 Now with awk: awk '$1 ~ /[ABCDEM]/ {print $0"\n"}$1 ~ /Question/ {print "\\item"}' inputfile If the line starts with A, B, C, D, E or M (for Main), it prints that line and an extra \n. If the line starts with "Question", it simply prints \item.

4

Your file starts with a UTF-8 byte order mark. It is unicode symbol U+FEFF which is encoded as three bytes in UTF-8. Those three bytes show up as 357 273 277 when you print them in base 8. To the sed command those bytes at the start of the line means that 1 is in fact not the first character on that line. Many other tools will treat it the same way. You ...

4

Using Gnu Awk: awk '/\<TML[0-9]*E\>/' log Match any word (field) on a line (record) that begins with TML, has any number of digits and then ends in E.

4

You came close. You need to suppress sed's output before selectively printing lines: $cal 12 2015 | sed -n '3,7p' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Your sed might not have -n. In which case, use d to delete all lines except the ones you want. ... 4 network="198.168.1.254" IFS="." read -r a b c d <<< "$network" next_serv="$a.$b.$c.$(($d-1))" echo "$next_serv" Output: 198.168.1.253

4

Let's consider this test file: $cat file \LeftLabel{foo} \LeftLabel{$foo} LeftLabel{foo} Now, let's make the substitution: $sed -E 's|\\LeftLabel\{([^$}][^}]*)\}|\\LeftLabel{\\textsf{\1}}|g' file \LeftLabel{\textsf{foo}} \LeftLabel{$foo} LeftLabel{foo} How it works The substitute command in sed looks like s|old|new|g where old is a regular ... 3 Looking at your snippet, it looks like you've got XML delimited by 'number='. So extract with split: #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; local$/; my @xml_chunks = split ( /\d+\=/, <> ); print Dumper \@xml_chunks; Of course, you should probably note - your XML ... isn't. The declaration isn't valid. I'm going to assume ...

3

The original question would be solved by cat test.xml | tr "<" "\n" | sed -n '/taga>./p' | sed 's/taga>//' the second, current question would be solved with cat test.xml | sed 's/[0-9][0-9][0-9][0-9]=/\n/g' it looks for an occurence of four numbers followed by an = sign, so if you might have those kinds of characters in other places in the real string ...

3

A perl approach (assuming your file is small enough to load into memory): perl -0pe 's/.+?\n.*?LinearRec$$1, F{58}$$.*?\n.*?\n//' file The -0 makes perl slurp the entire file, and the -p tells it to print each input line after applying the script given by -e. The script itself matches 58 Fs and the surrounding two lines and removes them.

3

sed -ne'$p;N;/^ *{ *\n *LinearRec(1, *FF*) *$/n;/\n/P;D' <in >out That gets sed a one-line look-ahead, and then only Prints to output any lines which don't fit your series, because it jumps one more and dumps the buffer when it matches the first two lines of it.

3

Can be done with awk: awk ' # define a long block BEGIN{ long = 10; } # output long block when new block is found ($1 == "query" && n >= long){ print s; } # new block ($1 == "query"){ s = ""; n = 0; } # all lines { s = (s != "") ? s "\n" $0 :$0; n++; } # output the long block if it is the last one in the file END{ if (n >= ...

3

sed -ie '/string1/!b' -e '/string2/d' file.txt That deletes lines that contain both string1 and string2 regardless of the order (may even overlap as when looking for foobar in barbaz in a line containing foobarbaz). -i above is a GNU extension. Another GNU-specific solution: awk -i inplace '!(/string1/ && /string2/)' file.txt Portably, you'd ...

3

Your interpretation is wrong. You told sed to replace something by something else; it it didn't find anything to replace, why should it delete anything? In other words, substitution replaces what matches, it doesn't touch non-matching lines. Or, yet another reformulation: sed correctly replaced all lines containing aa by aa.

3

Because you're using a single sed expression, everything that follows after the w (including the }) is interpreted as the wfile name: The argument wfile shall terminate the editing command. You can see that if you add a second command } e.g. like: sed -e '/my regex here/{w '"$file2"';d;}' -e '}'$file1 then the lines matching my regex here will be ...

3

I found the answer on Stackoverflow under "How do I let sed 'w' command know where the filename ends?" As terdon pointed out, the issue is not the variable—but it has nothing to do with curly brackets, either; try sed '/^l/w testing;p' and you will see it doesn't throw any error, but writes all the lines starting with an l into a file named testing;p. The ...

Only top voted, non community-wiki answers of a minimum length are eligible