0
ls -ltr /var/log/script1/
total 5964
-rw-r--r--. 1 root root  22720 Apr 28 11:14 controller.log
-rw-r--r--. 1 root root    869 Apr 28 11:14 controller.log.2018-03-26-14
-rw-r--r--. 1 root root    893 Apr 28 11:14 controller.log.2018-03-19-13
-rw-r--r--. 1 root root  22409 Apr 28 11:14 controller.log.2018-03-19-12
-rw-r--r--. 1 root root  36918 Apr 28 11:14 controller.log.2018-04-02-05
-rw-r--r--. 1 root root  62605 Apr 28 11:14 controller.log.2018-04-02-04

Above files contains multiple " at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)" messages I want to delete it and write the rest of the messages in same file

I am using below command which prints the output :

for f in /var/log/script1/* ; do sed '/^\s*at/d' $f  ; done

I tried below but no luck :

for f in /var/log/script1/* ; do sed '/^\s*at/d' $f > $f ; done

Above command return empty files. can some one please help !

Akshay
  • 31
  • 2
  • 1
    Are you looking for `sed`’s `-i` option? – Biffen Apr 28 '18 at 11:26
  • also, as a good practice, use `"$f"`.. or without loop: `sed /var/log/script1/*` – Sundeep Apr 28 '18 at 11:30
  • I was missing "-i" in sed command which did the trick. – Akshay Apr 28 '18 at 11:31
  • as to why you get empty files - https://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2Ffoo.2Fbar.2F_.3E_file – Sundeep Apr 28 '18 at 11:33
  • @Sundeep Thanks for the quick responce. Will use "$f" instead of $f. Is there a way after I delete I should be able to create files with same name and same content in a different dir. In this was I will have a backup of original files as well. – Akshay Apr 28 '18 at 11:35
  • @Biffen Thanks Could you please check ^^ – Akshay Apr 28 '18 at 11:35
  • @Akshay with GNU sed it is possible ([see my examples](https://github.com/learnbyexample/Command-line-text-processing/blob/master/gnu_sed.md#place-backups-in-directory)) not sure about other versions.. or save a copy to backup directory first within the for loop and then apply sed – Sundeep Apr 28 '18 at 11:36
  • @Sundeep checking. Thanks for your help :) – Akshay Apr 28 '18 at 11:39

0 Answers0