0

I want to delete a line with string 'Generation' in all the files under a folder named KCG. I tried the following in Windows OS command prompt:

sed -i '/Generation/d' file

But got the following error message:

sed: can't read file: No such file or directory

Next I tried:

sed -i '/Generation/d' airport_related_altitudes_derived_data.c

This worked, but I do not want to enter the filenames of all the files in the folder each time. Is there a command to recursively look for the string in all the files under the folder?

MUT
  • 13
  • 6
  • Please include your code as text, and [not an image](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). – Anonymous coward Oct 26 '18 at 20:23
  • 1
    sed and single quotes lets me assume a Linux OS - then the tag batch-file is wrong. –  Oct 26 '18 at 20:31
  • @LotPings: No I am using windows OS. I do not know the right syntax. Could you please suggest the right syntax for windows OS? – MUT Oct 29 '18 at 16:43
  • Normally replace single quotes (which have no special meaning in batch) with double quotes. My sed variant errors out with inplace edit (Gnu sed 4.1.2) but there are [others](https://stackoverflow.com/a/127567/6811411). –  Oct 29 '18 at 17:13
  • @LotPings: You mean the syntax is sed -i "/Generation/d" file? – MUT Oct 29 '18 at 18:17
  • Yes, but `file` should be a real file name. –  Oct 29 '18 at 18:19
  • But that's my requirement(as posted in the description). I do not want to type filename of each file in the folder. Hence I would like to know if there is any syntax to recursively remove any line(s) with string Generation in all the files under a given folder. – MUT Oct 29 '18 at 18:27
  • Check if it works with a single file, in batch you'll use a `for` loop do iterate all files of a type. –  Oct 29 '18 at 22:47
  • @LotPings: As mentioned in my original post, it works for single file even if I use single quotes. But as I said earlier, I do not want to enter filename each time. I want a syntax that runs recursively for all the .c and .h files under a given folder. – MUT Oct 30 '18 at 20:01

2 Answers2

0

Try this:

find . -type f -exec sed -i '/Generation/d' {} \;

That will recursively find all files ("-type f") under the current directory (".") and call "sed -i '/Generation/d'" on each file ("{}").

  • It did not work. Got an error message saying - FIND: Parameter format not correct – MUT Oct 27 '18 at 00:50
  • @user10564941, did you read the comment above from LotPings? The syntax you are using with `SED` tells us you are using a Linux which is why Anthony provided you a solutions using the `FIND` command from Linux. But the error message you just described in your comment tells me you are using Windows. – Squashman Oct 27 '18 at 04:19
  • @Squashman: Hi Squashman, yes I did read LotPings comment and that is why I have added more information in my original description mentioning that I am using Windows OS. Hence could you provide me a syntax for windows OS? – MUT Oct 29 '18 at 16:42
0

A windows way to enumerate files of a pattern/file type in current folder.

@Echo off
For %%A in (*.c) Do sed -i '/Generation/d' %%A

Recursive from a start path

For /r "X:\start\path" %%A in (*.c) do sed -i '/Generation/d' %%A

A pure batch way to drop lines containing words from a file is using find /v or findstr /v but requires a different output file name, no inplace editing.

@Echo off
for %%A in (*.c) do findstr /vi "Generation" "%%~A" >"%%~dpnA_new%%~xA"

what creates new files with a appended _new before the extension.

It's of course possible to rename the old to .bak and write the changed version to the original name.

  • Which command and on cmd line or in a batch file? To test oput precede `sed` with an `echo sed ...` –  Nov 01 '18 at 00:13