Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have multiple files with multiple links that are formatted like this:

<a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit">
    <img ... />
</a>

Bear in mind that FOO, BAR and FNORD can differ.

Now I want to make sure that all those files have the correct attributes set. For that purpose I want to use ack-grep and cut.

ack-grep -a ?parameter= *.txt

gettiting the output:

fileName.txt
1:<a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit">

fileName2.txt
5:<a href="http://example.com/fnord.layername.html?parameter=POIT-_-NARF-_-BAZINGA" class="poit">

Yet I am at a total lost about how I could cut the output so that I would only see:

fileName.txt
parameter=FOO-_-BAR-_-FNORD

fileName2.txt
parameter=POIT-_-NARF-_-BAZINGA
share|improve this question

3 Answers 3

up vote 1 down vote accepted

I don't have ack-grep utility on my mac so will this awk solution work for you?

awk -v FS=[?\"] '/param/{print FILENAME; print $3 }' INPUT_FILE

Execution:

[jaypal:~/Temp] cat file0
<a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit">
    <img ... />
</a>

[jaypal:~/Temp] awk -v FS=[?\"] '/param/{print FILENAME; print $3 }' file0
file0
parameter=FOO-_-BAR-_-FNORD

Probable work around for you:

If getting the filename along with parameter values is critical, then you can either run this with a simple for loop or may be you can redirect the output you are getting currently in to a file or pipe it to the following awk one-liner.

Simple for loop:

[jaypal:~/Temp] for i in ./file*.txt; do awk -v FS=[?\"] '/param/{print FILENAME; print $3"\n" }' "$i"; done
./file01.txt
parameter=FOO-_-BAR-_-FNORD

./file02.txt
parameter=POIT-_-NARF-_-BAZINGA

Redirecting your current output to a file and invoking awk on it:

Following example assumes redirection of your current output to a file called temp.file.

[jaypal:~/Temp] cat temp.file 
fileName.txt
1:<a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit">

fileName2.txt
5:<a href="http://example.com/fnord.layername.html?parameter=POIT-_-NARF-_-BAZINGA" class="poit">

[jaypal:~/Temp] awk -v FS=[?\"] 'NF==1{ print $0 }/param/{ print $3"\n" }' temp.file 
fileName.txt
parameter=FOO-_-BAR-_-FNORD

fileName2.txt
parameter=POIT-_-NARF-_-BAZINGA
share|improve this answer

You can use the --output EXPR argument to ack-grep to selectively print just part of the line. Keep in mind that ack-grep is a Perl script, the EXPR argument is just any Perl expression; in this case I use parentheses in the pattern match to grab just the part of the line from the ? to the closing " (I also use backslash to quote the regexp metachar '?' which you didn't do, although perhaps as a first character in a regexp that might happen to work).

ack-grep -a --output '$1' '\?(parameter=.*?)"'

Note the last ? in the regexp makes the .* non-greedy, so that it matches up to the first " and not the last one on the line.

This generates output lines like 1:parameter=FOO-_-BAR-_-FNORD - you can suppress the leading 1: with the -h argument to ack-grep.

share|improve this answer

I'm not sure I understand, and I'm not familiar with the ack-grep utility, but why don't you try cutting from the first occurrence of ? to the " ?

Something like the following:

For readability reason, I put the html tag in a variable

var='<a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit">'

cut -d '?' -f 2- <(echo $var ) | cut -d '"' -f 1
share|improve this answer
    
FOO, BAR and FNORD can be different. –  k0pernikus Nov 28 '11 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.