Skip to content
Advertisement

Deleting strings from text file

I have a txt file, like this:

| abc_0123        | CREATE_COMPLETE      | 2020-02-17T12:03:27  | D4F5                        |
| defgh_0456       | CREATE_COMPLETE      | 2020-02-28T13:25:15  | G6H7I8
…

I only want to keep:

abc D4F5
defgh G6H7I8

I tried:

sed -i 's/| abc_0123        | CREATE_COMPLETE      |/abc/g' text.txt

It works, but can’t get rid of the date.

Advertisement

Answer

$ cat input
| abc_0123        | CREATE_COMPLETE      | 2020-02-17T12:03:27  | D4F5          
| defgh_0456       | CREATE_COMPLETE      | 2020-02-28T13:25:15  | G6H7I8
$ awk '{split($2,a, "_"); print a[1], $5}' FS=| input
 abc  D4F5                        
 defgh  G6H7I8

Although you might prefer to get rid of more whitespace with:

awk '{split($2,a, "_"); print a[1], $5}' FS='[| ]*' input
Advertisement