20

Is there any cut command to cut based on a word eg :

171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD. The task has terminated abnormally because of a program check. 16:08:27

I want the output as :

171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD.

How to approach?

3
  • 2
    Which word do you mean?
    – Cyrus
    Dec 13, 2017 at 4:01
  • ABCD is the word which i want to use as delimiter. And also, this is a log, and like ABCD, i am having someother keywords too. I want an approach in general
    – Anony
    Dec 13, 2017 at 4:02
  • 1

5 Answers 5

27

I suggest:

awk -F 'ABCD' '{print $1 FS "."}' file

Output:

171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD.

FS contains your delimiter "ABCD". "ABCD" is a regex.

1
  • +1 for the solution. I wanted to get the sentence after the word so, I did '' awk -F 'ABCD' '{print $2 FS }' file '' ; so I got "The task has terminated abnormally because of a program check. 16:08:27" ; Thanks once again :) Aug 9, 2021 at 12:48
7

sed version:

sed 's/ABCD.*/ABCD./' input.txt
171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD.

Source: https://unix.stackexchange.com/questions/257514/how-to-delete-everything-after-a-certain-pattern-or-a-string-in-a-file

2
  • I've changed it, but would it make a difference?
    – user689314
    Dec 13, 2017 at 4:26
  • It would still work - just pointing out it's not necessary Dec 13, 2017 at 4:29
6

awk can do the job if you provide -F flag with the word which you want to use:

$ awk -F 'test'  '{print $1;print $2}'  <<<  "onetesttwotest"                                                            
one
two

In your particular case, that would be:

$ awk -F 'ABCD' '{print $1,FS}' input.txt                                                                                
171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend  ABCD

Judging from your example, you're only trying to print stuff up to ABCD so deleting everything after that is also an option:

$ awk '{print substr($0,0,match($0,/ABCD/)+4);}' input.txt                                                               
171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD.
3
  • They also want to keep the 'ABCD.'
    – user689314
    Dec 13, 2017 at 4:08
  • Yea..ABCD must be there
    – Anony
    Dec 13, 2017 at 4:08
  • I suggest awk '{print gensub(/(.*ABCD\.).*/,"\\1",1)}'.
    – dessert
    Dec 13, 2017 at 11:28
3

grep solution:

grep -o '^.*ABCD\.' input.txt

The regular expression will match to each line that begin ^ with any character . repeated number of times * and ends with the string ABCD. The backslash \ escapes the special meaning of the dot . at the end.

The option -o will tell the grep command to print only the matched (non-empty) parts of a matching line.

3

cut can itself perform this job. Not based on a word, but based on . delimiter.

Use :

cut -f 1 -d '.' input.txt | xargs -I "%" echo %.

Output

rooney@bond-pc:~$ cat input.txt 
171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD. The task has terminated abnormally because of a program check. 16:08:27
rooney@bond-pc:~$ 
rooney@bond-pc:~$ cut -f 1 -d '.' input.txt | xargs -I "%" echo %.
171212 16082784       6264 XXX     xxxxxxxx Transaction XXXXX abend ABCD.

xargs is used here only to append . at the end of string ABCD.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .