How to Extract a Single Quote from a file Using GREP

B

Ben Nguyen

I have some text files that on the 12th line of each file has a sentence
in quotes.

Id like to extract the sentence (without the quotes) from all
the files and put them into a single new file.

Seems like grep is designed for this type of thing, but not exactly sure
how to enter it :(

Ben
 
W

Walter Roberson

:I have some text files that on the 12th line of each file has a sentence
:in quotes.

:Id like to extract the sentence (without the quotes) from all
:the files and put them into a single new file.

:Seems like grep is designed for this type of thing, but not exactly sure
:how to enter it :(

# untried
print NEWFILE grep { close ARGV if eof; $. == 12 ? s/^.*"(.*)".*/$1\n/ : 0 } (<>)

The close of ARGV is needed to reset the $. counter upon EOF of
each of the individual ARGV files. The $. == 12 tests for line 12.
The substitute changes the implicit $_ string in place.
Lines other than 12 have 0 returned from the BLOCK. grep makes
a list of the $_ values for which the expression was true.
You then print out that list. The line seperators were supplied
in the replacement pattern of the s/// operator.

If the 12th line has strange quotings, then you'll get strange
results. If the line does not have two double-quotes then the s///
operator will return 0 and that line will be excluded from the list.
 
A

Anno Siegel

Walter Roberson said:
:I have some text files that on the 12th line of each file has a sentence
:in quotes.

:Id like to extract the sentence (without the quotes) from all
:the files and put them into a single new file.

:Seems like grep is designed for this type of thing, but not exactly sure
:how to enter it :(

# untried
print NEWFILE grep { close ARGV if eof; $. == 12 ? s/^.*"(.*)".*/$1\n/ :
0 } (<>)

That won't work for at least two reasons. One is that s/// changes
$_, but returns a boolean, not the changed value of $_.
The close of ARGV is needed to reset the $. counter upon EOF of
each of the individual ARGV files. The $. == 12 tests for line 12.
The substitute changes the implicit $_ string in place.

$. and the related actions are useless in this kind of loop. <> in
list context reads all of STDIN in one go, and $. goes to its final
value. The operations you are trying work in a while-loop:

while ( <> ) {
close ARGV if eof;
next unless $. == 12;
s/.*"(.*)".*/$1/;
print;
}

The substitution lets the linefeed intact (why?). Your code would have
introduced an extra one. But it is better to extract the text without
an un-necessary change to $_: 'print "$1\n" if /"(.*)"/' does it in
one step and with less technology.

Anno
 
U

Uri Guttman

AS> while ( <> ) {
AS> close ARGV if eof;
AS> next unless $. == 12;
AS> s/.*"(.*)".*/$1/;
AS> print;
AS> }

AS> The substitution lets the linefeed intact (why?). Your code would have
AS> introduced an extra one. But it is better to extract the text without
AS> an un-necessary change to $_: 'print "$1\n" if /"(.*)"/' does it in
AS> one step and with less technology.

why do a s/// instead of //? and why loop to eof? if you have read the
12th line close the file

while ( <> ) {
next unless $. == 12;
print /.*"(.*)".*/, "\n" ;
close ARGV ;
}

uri
 
A

Anno Siegel

Uri Guttman said:
AS> while ( <> ) {
AS> close ARGV if eof;
AS> next unless $. == 12;
AS> s/.*"(.*)".*/$1/;
AS> print;
AS> }

AS> The substitution lets the linefeed intact (why?). Your code would have
AS> introduced an extra one. But it is better to extract the text without
AS> an un-necessary change to $_: 'print "$1\n" if /"(.*)"/' does it in
AS> one step and with less technology.

why do a s/// instead of //?

I suggested print "$1\n" if /"(.*)"/
and why loop to eof? if you have read the
12th line close the file

Yes, nice improvement.
while ( <> ) {
next unless $. == 12;
print /.*"(.*)".*/, "\n" ;

print /"(.*)"/, "\n" ;
close ARGV ;
}

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top