ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

delete all the lines that do NOT start with a specific string in multiple files

(1/4) > >>

kalos:
hello

I have an amount of text files and I want to delete all the lines that do NOT start with a specific string

how can I do this?

thanks

EDIT: the program must support unicode characters

housetier:
on linux a combination of grep and the shell's for loop could accomplish this:


--- ---export BAN="whatever should not be kept"
mkdir after
for textfile in *.txt; do grep $textfile -v ^$BAN > after/$textfile; done

This should work if the files all have a .txt extension.

"grep -v" prints lines that do NOT match; "^" means beginning-of-line; "$BAN" will be replaced by whatever you put between the quotation marks in the export statement above. The output from grep is then put into a new file with the same filename, but in a different directory.

If you have cygwin, you can do this under windows as well.

bgd77:
kalos, could you specify if this is for Windows or Linux?

TucknDar:
edit:
completely misunderstood... sorry

kalos:
it's for winxp
thanks

Navigation

[0] Message Index

[#] Next page

Go to full version