Delete a part of a line with sed?

8,226

Solution 1

Using sed

sed -i -e 's/\*.*//g' -e 's/ IP//g' -e '\$^Syntax.*$d' File*

This should also work:

awk '/Date/{print $0} /^Product|^Soft/{print $1" "$2" "$3} /^$/' File*

Solution 2

Here's one way with awk. The NR variable represents the record number; just print it if its the first. awk counts fields from one, so you are saying you only want the first three thereafter:

awk 'NR==1 {print;next};{print $1,$2,$3}' 

Solution 3

Using sed to delete the last word and any spaces or tabs before it on lines 5 and onwards:

$ sed -E '5,$s/[[:blank:]]*[^[:blank:]]+$//' file
Date Time: June 22 12:40:00

Product User Host IP

Software User User
Software1 User User

The 5,$ addresses every line from line 5 onwards, and the substitution that acts on these lines will remove any number of spaces or tabs followed by something that is made up from non-spaces and non-tabs at the end of the line.

Share:
8,226
Mauricio Reyes
Author by

Mauricio Reyes

KSH Lover.

Updated on September 18, 2022

Comments

  • Mauricio Reyes
    Mauricio Reyes over 1 year

    I have the next output:

    Date Time: June 22 12:40:00
    
    Product User Host IP
    
    Software User User *.***.***.***
    Software1 User User *.***.***.***
    

    I tried some functions with sed and awk, but is not working; I want that the text stays at the next way:

    Date Time: June 22 12:40:00
    
    Product User Host
    
    Software User User 
    Software1 User User
    

    I have the next command:

    $ cat Mathematica_06-* | \
      grep -w -A 4 -B 5 "AER8\|User\|User1\|User2\|User3\|User4\|User5\|User6\|User7\|User7\|User8\|User9\|User10\|User11\|User11\|User12\|User13\|User14\|User15\|User16\|User17\|User18\|User19" | \
      awk '{print $1,$3,$4,$5,$7}' | \
      egrep -v
        > Output.log
    

    I tried to add at final egrep -v "|IP" but if I put the line in that command, I obtained the next output:

    Date Time: June 22 12:40:00
    
    Softare User User 
    Software User User
    

    Is there any other way to delete the "IP" and the "*" parts without affect the rest of the line?

    • jesse_b
      jesse_b almost 6 years
      Are the *.***.***.*** literal or have you redacted actual IP addresses?
    • Mauricio Reyes
      Mauricio Reyes almost 6 years
      Yes is a public IP, I don't want make public but the line contains the same length