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
4
  • Could you please reformat your question with code tags? As it is now it is hard to read. Sep 29, 2016 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 Sep 29, 2016 at 11:26
  • txr -e '(awk (:begin (set fs ",")) ((del [f 19])))
    – Kaz
    Sep 30, 2016 at 0: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, 2016 at 0:39

3 Answers 3

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.

1
  • 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, 2016 at 11:52
0

This should work:

grep Event: a.txt | awk 'BEGIN{FS=OFS=","}{$20=""; print}' > asort.txt
5
  • @don_crissti I Modified it to preserve FS
    – jcbermu
    Sep 29, 2016 at 11:41
  • this leaves an extra , of the deleted column, which may or may not be what OP required... for ex: compare - echo 'a,b,c,d' | cut -d, -f1,3- to echo 'a,b,c,d' | awk 'BEGIN{FS=OFS=","}{$2=""; print}'
    – Sundeep
    Sep 29, 2016 at 11:47
  • 2
    To do it "properly" in awk: awk -F, -v OFS=, '{for (i=21; i<=NF; i++) $(i-1)=$i; NF--; print}' Sep 29, 2016 at 13:03
  • @glennjackman It works. You should put it as an answer.
    – jcbermu
    Sep 29, 2016 at 13:10
  • I think cut is the better solution. Feel free to include it in your answer. Sep 29, 2016 at 13:15
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

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