Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 887  / 1 Year ago, thu, november 17, 2022, 3:34:58

I have the file "test.txt" that contains:


Val1 = '59'
Val2 = '76'
Val3 = '42'
Val4 = '53'

I with this command:


perl -pe "s/^Val2 = '(.*)'/1/" test.txt

I Want:


76

But I obtain:


Val1 = '59'
76
Val3 = '42'
Val4 = '53'

More From » bash

 Answers
6

The -p argument is like sed's default print - also like sed, if you want to suppress default print, you would use -n instead.


So you could do


perl -ne "print if s/^Val2 = '(.*)'/1/" test.txt

You could also use a regex match rather than a regex substitute:


perl -lne "print $1 if /^Val2 = '(.*)'/" test.txt

or


perl -nE "say $1 if /^Val2 = '(.*)'/" test.txt

(the backslash is to protect $1 from being expanded by the shell, since the expression is in double quotes to allow use of lieral single quotes in the match).


[#2423] Thursday, November 17, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ampolinhad

Total Points: 88
Total Questions: 100
Total Answers: 116

Location: South Georgia
Member since Tue, Feb 1, 2022
2 Years ago
ampolinhad questions
Thu, Sep 8, 22, 15:45, 2 Years ago
Tue, Aug 10, 21, 20:03, 3 Years ago
Sat, Oct 16, 21, 22:44, 3 Years ago
Sat, Oct 23, 21, 03:11, 3 Years ago
Mon, Nov 14, 22, 22:15, 1 Year ago
;