0

Content of file a.txt

Event: "112506400","17","2016/07/13-15-25-59.00",,,,,,,,,,,"112506400","115101234","02:00:00","pc","abc","4194","file_nam","F",,,"LA
",,"jk","123",,,,,,,,,,

I need a file which doesn't have $20 ( file_name ) redirected to asort.txt . Is there any short command as currently I am using the below

cat a.txt | grep Event: |awk -F, '{print $1","$2","$3","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13","$14","$15","$16","$17","$18","$19","$21","$22","$23","$24","$25","$26","$27","$28","$29","$30","$31","$32","$33","$34","$35","$36","$37}'> asort.txt
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Could you please reformat your question with code tags? As it is now it is hard to read. –  Sep 29 '16 at 11:21
  • 1
    Probably easier with cut e.g.cut -d, -f1-19,21- a.txt or (with GNU) cut -d, --complement -f20 a.txt – steeldriver Sep 29 '16 at 11:26
  • txr -e '(awk (:begin (set fs ",")) ((del [f 19]))) – Kaz Sep 30 '16 at 00:37
  • txr -e '(awk (:begin (set fs ",")) (t (del [f 19]))) (If some records don't have 20 or more fields and must be printed, we need an always-true condition! del returns the prior value of the deleted place, which is nil if there is no [f 19], and nil is Boolean false). – Kaz Sep 30 '16 at 00:39

3 Answers3

2

Maybe the cut command can do:

cat a.txt | cut -d "," -f 1-19,21-37

Thus you skip field #20, assuming the comma is the stable delimiter.

Kate
  • 849
  • 2
    you could improve by passing file name directly to cut and no need to hard code final column number.. so something like cut -d "," -f 1-19,21- a.txt :) – Sundeep Sep 29 '16 at 11:52
0

This should work:

grep Event: a.txt | awk 'BEGIN{FS=OFS=","}{$20=""; print}' > asort.txt
jcbermu
  • 4,736
  • 18
  • 26
0

with sed

$ echo 'a,,b,c,d' | sed -E 's/^(([^,]*,){2})[^,]*,/\1/'
a,,c,d
$ echo 'a,,b,c,d' | sed -E 's/^(([^,]*,){3})[^,]*,/\1/'
a,,b,d
  • [^,]*, zero or more non comma text followed by comma
  • {2} or {3} preceding group two or three times, use column number to delete minus one

similar with perl except that we can reuse regex pattern

$ # (?2) refers to ([^,]*,)
$ echo 'a,,b,c,d' | perl -pe 's/^(([^,]*,){2})(?2)/$1/'
a,,c,d
$ echo 'a,,b,c,d' | perl -pe 's/^(([^,]*,){3})(?2)/$1/'
a,,b,d

$ # golfed with lookbehind
$ echo 'a,,b,c,d' | perl -pe 's/^([^,]*,){2}\K(?1)//'
a,,c,d
$ echo 'a,,b,c,d' | perl -pe 's/^([^,]*,){3}\K(?1)//'
a,,b,d
Sundeep
  • 12,008