If I have a log file and want to dump only the text between 1234 and 9876 in to another file, how can i do this easily?
If I have a text file like this:
idsfsvcvs sdf sdf e e sd vs d s g sg s vc d
slkdfnls 1234 keep me text 9876 das a g w eg dsf sd fsdf
sdfs fs dfsdf
sdfsdf sdf
sdf s fs
dfsf ds
I want to do somthing like this
$ getinfo "1234" "9876" log
$ cat log
keep me text
One line of sed can do this for you:
sed -n 's/.*1234 \(.*\)9876.*/\1/p' textfile.txt > log
normally you can do this with grep and the -o param.
so it would be something like:
Not 100% sure about the regex btw, I did not test it
This depends on your file content. You can start by something simple like:
This works for the provided example. You can work on it to cover other data formats if needed. You can also redirect the output to a file by appending
> /path/to/output
For what it's worth, I would do the following:
grep 1234.*9876 > myfile
vim myfile
:%s/^.*1234//
(delete everything up to 1234):%s/9876.*$//
(delete everything after and including 9876)Not perfect, or the best way, but easy to remember if you often use vim.