10

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?

  • 1
    Which word do you mean? – Cyrus Dec 13 '17 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 '17 at 4:02
  • 1
18

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.

4

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.
  • They also want to keep the 'ABCD.' – user689314 Dec 13 '17 at 4:08
  • Yea..ABCD must be there – Anony Dec 13 '17 at 4:08
  • I suggest awk '{print gensub(/(.*ABCD\.).*/,"\\1",1)}'. – dessert Dec 13 '17 at 11:28
4

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

  • I've changed it, but would it make a difference? – user689314 Dec 13 '17 at 4:26
  • It would still work - just pointing out it's not necessary – steeldriver Dec 13 '17 at 4:29
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.