2

I want to apply this sed command

sed '/begin 644/,$d' file1.txt > file1.txt

to all files of the directory.. basically I want to keep the having the same name but deleting all lines after a certain string is found.. how can i adjust this sed command to be applied to all the text (.txt) files in the folder and keep their original names ?

EDIT : I am using Mac OS X I don't know if there is some issue with the sed command...

if i try to do

sed -i '/begin 644/,$d' *.txt

i get an error sed: 1: bad flag in substitute command : 'x'

2nd EDIT : Anu's answer works !

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • Possible duplicate of http://stackoverflow.com/questions/12696125/sed-edit-the-file-in-place – AlG Feb 19 '15 at 20:10
  • Haha thanks man but is it somehow special ? :D I was waiting for it tho :D – adrCoder Mar 07 '15 at 10:45
  • I don't know, I've forgotten at which levels you get extra abilities on SO. Unless your pages are now showing alerts at the top, I guess the answer is 'no'. ;) – Andrew Thompson Mar 07 '15 at 10:47
  • A "review" button appeared in the top and here http://stackoverflow.com/help/privileges it says i can review posts whatever that means :D – adrCoder Mar 07 '15 at 10:49

3 Answers3

3

You can use this find with sed:

find . -maxdepth 1 -name '*.txt' -exec sed -i.bak '/begin 644/,$d' {} + 

Or if you want to keep begin 644:

find . -maxdepth 1 -name '*.txt' -exec sed -i.bak -n '1,/begin 644/p' {} + 
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • hi anu. i want to delete the lines AFTER the match is found.. for example, if begin 644 is written in line 100, i want to delete all lines from 100 till the end.. i think you need to change your regex – adrCoder Feb 19 '15 at 20:20
  • `sed -n '1,/begin 644/p' file` actually does same but I will change it your sed command. – anubhava Feb 19 '15 at 20:23
  • it is not working, it is creating a .bak file which is the original file and then a file without the .bak name which is empty.. EDIT : I am using Mac OS X maybe there is some issue with the sed command ? – adrCoder Feb 19 '15 at 20:28
  • This is to cater OSX ( I am also using OSX). Try: `find . -maxdepth 1 -name '*.txt' -exec sed -i.bak '/begin 644/,$d' {} \;` – anubhava Feb 19 '15 at 20:32
  • now it creates 2 files which are the same as the original.. one example of a line after which I want to delete: begin 644 g274705g08j38.jpg . Maybe there is some issue with the regex or else why is not deleting the lines after the match? – adrCoder Feb 19 '15 at 20:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71292/discussion-between-anubhava-and-adrcoder). – anubhava Feb 19 '15 at 20:35
  • hey anu i am trying ur code but if there are many files in the folder only one is processed and the others are empty (zero bytes).. do you know how to fix that ? – adrCoder Feb 20 '15 at 18:50
  • `find . -maxdepth 1 -name '*.txt' -exec sed -i.bak '/begin 644/,$d' {} \;` should run on all matching `*.txt` files – anubhava Feb 20 '15 at 18:52
  • even if the folder has only 2 files, it only processed 1, the other the .txt file is empty and a .bak is created which has the contents of the original .txt file.. – adrCoder Feb 20 '15 at 19:03
  • 1
    Sorry, sorry, the command you just wrote in the comments works . Thanks :D – adrCoder Feb 20 '15 at 19:10
2

Just use in-place version of sed as

sed -i '/begin 644/,$d' file1.txt
unxnut
  • 8,509
  • 3
  • 27
  • 41
  • 1
    On MacOsX you will have to use 'sed -i.random-file-extension', because their version of sed is ancient. – pelya Feb 19 '15 at 20:18
1

You can use the edit in place sed option and the star selector like so

sed -i '/begin 644/,$d' *.txt
ShaneQful
  • 2,140
  • 1
  • 16
  • 22