Read from a certain point to a certain character

Say I have a file like this:

foo bar foo bar foo bar foo bar something useful"foo bar foo bar"

Basically, I want to know how I can get the string something useful by itself, either saved to its own file or displayed as output alone.

There will always be the same number of characters (33) before something useful and there will always be " directly after it.

Asked By: MD XF

||

Try this:

cut -c 34- | cut -d '"' -f1

First cut removes the first 33 characters; second cut keeps only the part before the first ".

Answered By: xhienne

A vim solution:

vim test.txt -c '%s/v.{32}(.{-})".*/1' -c 'w output.txt' -c 'q!'

Saves the result in output.txt, which obviously could be changed.

Answered By: DJMcMayhem

Here is GNU grep version using perl syntax:

grep -oP '.{32}K[^"]*'

it greps first 32 characters, cuts it away with K and prints the rest until first ".

Answered By: jimmij

You can use sed to achieve your task

sed -e 's/^.{32}//;s/".*//' filename.txt

This removes the first 32 characters and removes everything after the first ".

These substitutions will occur on every line of the file. To apply this to a specific line, use this:

sed -e 'linenumber{s/^.{32}//;s/".*//;}' filename.txt

where linenumber is any number.

linenumber{          # on the linenumber-th line, do the following
 s/^.{32}//        #  remove the first 33 characters
 s/".*//             #  remove the first quote and everything following it
}
Answered By: user41805

In bash and parameter expansion expression

$ nline="${line%%"*}" #strip everything from first " seen to the end
$ echo "${nline:32}"   #print everything from offset 32 to the end
Answered By: αғsнιη
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.