Hot answers tagged

6

Here's one way to do it with sed: sed '/PATTERN1/,$!d;/PATTERN2/{x;//{x;q;};g;}' infile This just deletes all lines (if any) up to the first occurrence of PATTERN1 and then, on each line that matches PATTERN2 it exchanges buffers. If the new pattern space also matches, it means it's the 2nd occurrence so it exchanges back and quits (after auto-printing). ...


5

Just use flags: $ awk '/PATTERN1/{flag=2;next} flag; /PATTERN2/{flag--}' file a b c PATTERN2 d e f PATTERN2 That is: when you find PATTERN1 set the flag to a positive value; in particular, 2. Then, when you find PATTERN2, decrease that flag in one. This way, it will exhaust after the second match. In between, use flag as a value that triggers the {print $...


5

The right tool for this job is pcregrep: pcregrep -M 'PATTERN1(.|\n)*PATTERN2' file where option -M allows pattern to match more than one line and (.|\n)* match any character or newline zero or more times. Notice that we took advantage of the greediness of the grep. If you would want to print form pattern1 up to the first occurrence of the pattern2, ...


4

There are many ways but one from several can be: sed '/SSL/,/#ApiText/{//b;/^#/d}' 1.txt /SSL/,/#ApiText/ - address range where commands will execute // - for line(s) with above noted regexp (SSL or #ApiText - any of found) b - go to end of instructions ( mean do nothing) /^#/d - delete(do not print) lines which starts by #


3

With gnu sed/shuf: sed '1b;s/^*/\x0*/' infile | shuf -zn 5 | tr -d '\000' This turns input into nul separated records i.e. on each line that starts with a * (except for the first one) it adds a nul char before the * then uses shuf with --zero-terminated switch to extract five random records and tr to delete those nul chars.


3

Perhaps you meant to use less on the output of sed, rather than the reverse: sed -e 's/\(.*DATA.*\)/\o033[93m\1\o033[39m/' file | less -R Further reading: less - opposite of more


2

If we told regarding sed is to much easy to collect nesessary lines then print sed -n ' /PATTERN1/{ :1 $!N /\(PATTERN2\).*\1/!b1 p } ' file


2

Here's one way to do it with sed: sed '/Os version rhel5\.6/{ a\ apache 4.2 $!{ n /^apache 4\.2$/d } }' infile This appends apache 4.2 unconditionally to all lines matching Os version rhel5.6 then (if not on the last line) it pulls in the next line via n (printing the pattern space) and if the new pattern space content matches apache 4.2 it deletes it. ...


2

This perl expression will do the trick, perl -i -ne 'next if /apache 4.2/;s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print' ssss Explanation next if /apache 4.2/ skips any lines matching apache 4.2. s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print search Os version rhel5.6 and replaces line with same with appending apache 4.2 at ...


2

cat listfile | tr '\n' , | sed 's/,\*/\n*/g;s/,$//' | shuf | head -n 5 | tr , '\n'


1

Assuming that they are always the same amount of lines, you could do something like this: sed '/Connect\s*user@localhost on/,+7d' log.file This will remove the line containing Connect user@localhost on and the following 7 lines from the file "log.file" in your current directory. Edit: final solution (well, at least good enough for the OP to alter to his ...


1

The problem was that my editor was being stupid and I thought it was word wrapping. When I put sed -i.bak -c 's|net.ipv4.ip_forward.*|net.ipv4.ip_forward = 1|' /etc/sysctl.conf Into the editor the "/etc/sysctl.conf" would be touching the edge of the window so it displayed part of it on the next line. I thought it was just word wrapping but nope. Centos ...


1

If your system's grep supports PCRE, you could maybe do $ echo 'aaa string1 bbb aaa string2 bbb aaa string3 bbb' | grep -oP '(?<=(aaa|bbb) )\w*?(?= (aaa|bbb))' string1 string2 string3 or if you need to handle more general amounts of surrounding whitespace $ echo 'aaa string1 bbb aaa string2 bbb aaa string3 bbb' | grep -oP '(aaa|bbb)\s+\K\w*?(?=\s+...



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