Cut command with word as delimiter
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?
Solution 1:
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.
Solution 2:
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
Solution 3:
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.