Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

if specifc line contains substring then output another substring

$
0
0

I want to look for a string on every line of a file and if it exists, return a different specific string.

I have this code:

Numlines=$(grep "" -c File.txt)for (( line=1; line<=$Numlines; line++ )) ; do awk -v line="$line" 'NR==line ...???

The string I am looking for is style-name="T. If that string is in a line in the for loop, return the digits that are directly after the T. The lines in File.txt may contain strings like style-name="T2", in which case I want to return just the 2. The string is not located in the same place on every line in File.txt, so I don't think I can use field designations in awk.

IIRC, "/style-name\=\"T/" should provide the match, but if I use that in my code, it either gives me an error or returns nothing. Maybe the script would check to see if it could produce a match and, if so, use a second line of code to to get the string, although I think that awk can accomplish it with a single line of code, once the precursor code is figured out.

Here is a sample of File.txt:

<TEST1>     <text:p text:style-name="P4">Hello<text:span text:style-name="T2">world</text:span></text:p><TEST2>     <text:p text:style-name="P9">Hi<text:span text:style-name="T16">there</text:span></text:p><TEST2>     <text:p text:style-name="P540">0 <text:s/>oooh yeah<text:s text:c="2"/>kool-aid<text:s text:c="12"/>0:00</text:p>

The output for the first line (first time through the for loop) should be 2. The output for the second line (second time through the for loop) should be 16. The output for the third line should be nothing.


Viewing all articles
Browse latest Browse all 102

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>