1

I've been reading about this in Combining two sed commands , https://unix.stackexchange.com/questions/407937/combine-two-sed-commands , combine two sed commands, and other questions but still don't get how to combine 2 sed commands.

Here is my sample file (file.txt).

    10/8/18
6:54:42.000 PM  
Oct  8 19:54:42 x.x.x.x 231 <134> 2018-10-08T18:54:42Z  Server_Name: 2018-10-08 18:54:42 - Server_Name - [127.0.0.1] System()[] - User Accounts modified. Removed username JohnDoe from authentication server RSA.

    host =  Server_Name 
    source =    /opt/x.x.x.x-20181008.log   
    sourcetype =    sslvpn  

    10/8/18
6:47:33.000 PM  
Oct  8 19:47:33 x.x.x.x 266 <134> 2018-10-08T18:47:33Z  Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Closed connection to z.z.z.z after 6547 seconds, with 5526448 bytes read and 15634007 bytes written 

    host =  Server_Name 
    source =    /opt/x.x.x.x-20181008.log   
    sourcetype =    sslvpn  

    10/8/18
6:47:33.000 PM  
Oct  8 19:47:33 x.x.x.x 229 <134> 2018-10-08T18:47:33Z  Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - VPN Tunneling: Session ended for user with IPv4 address z.z.z.z

    host =  Server_Name 
    source =    /opt/x.x.x.x-20181008.log   
    sourcetype =    sslvpn  

    10/8/18
6:47:33.000 PM  
Oct  8 19:47:33 x.x.x.x 204 <134> 2018-10-08T18:47:33Z  Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Logout from y.y.y.y (session:abc)

    host =  Server_Name 
    source =    /opt/x.x.x.x-20181008.log   
    sourcetype =    sslvpn  

Desired Output

Oct  8 19:54:42 x.x.x.x 231 <134> 2018-10-08T18:54:42Z  Server_Name: 2018-10-08 18:54:42 - Server_Name - [127.0.0.1] System()[] - User Accounts modified. Removed username JohnDoe from authe
ntication server RSA.

Oct  8 19:47:33 x.x.x.x 266 <134> 2018-10-08T18:47:33Z  Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Closed connection to z.z.z.z after 6547 seconds, with 5526448 by
tes read and 15634007 bytes written

Oct  8 19:47:33 x.x.x.x 229 <134> 2018-10-08T18:47:33Z  Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - VPN Tunneling: Session ended for user with IPv4 address z.z.z.z

Oct  8 19:47:33 x.x.x.x 204 <134> 2018-10-08T18:47:33Z  Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Logout from y.y.y.y (session:abc)

What I did (2 sed commands) to produce the output was

sed -n '/[0-9]\/[0-9]\//,+1!p' file.txt > file2.txt
sed -n '/host =/,+3!p' file2.txt

Based on the answer on the other questions, semi colon was the solution but I just not sure how to use it. Here is my attempt to use semi colon which didn't work at all.

sed -n '/[0-9]\/[0-9]\//,+1!p;/host =/,+3!p' file.txt

Please advise

2 Answers2

1

Why not modify your sed to just match the pattern and delete rather than defining negate conditions? just delete the lines using the d operator

sed -e '/[0-9]\/[0-9]\//,+1d' -e '/host =/,+3d' file

I would wager a guess that the -n flag in your attempt could be the possible reason for not able to combine the two constructs together. Because by definition the -n flag would let sed print only the parts that are matched and not print every other line.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • wow! this is perfect. tested it with actual log file and it works. Anyway, I still want to know how to combine the previous 2 sed commands .. just for learning purpose. Thanks –  Oct 09 '18 at 07:12
  • 2
    @Sabrina you'll need `sed -n '/[0-9]\/[0-9]\//,+1{!p;b};/host =/,+3!p'` so that the first command doesn't affect later condition checks.. as mentioned by Inian, deleting is better choice here... also, see https://stackoverflow.com/questions/5864146/use-slashes-in-sed-replace – Sundeep Oct 09 '18 at 08:23
0

You got the answer to your question but you're using the wrong tool the wrong way. There's so many better ways to do what you want than specifying 2 different negative conditions (i.e. testing for what you don't want output rather than what you do). For example:

grep -A 1 --no-group-separator '^[[:alpha:]]' file

awk '(NR%8) ~ /^[34]$/' file

awk '/^[[:alpha:]]/{c=2} c&&c--' file

or if you really feel a burning need to use sed:

sed -n '/^[[:alpha:]]/,/^/p' file

In general - avoid negative logic because it's harder to understand (and usually maintain) than positive logic and introduces the risk of double negative logic which is extremely confusing and error-prone.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185