Maira  Zarate
Maira Zarate

Reputation: 1

Curl - Printing value using grep

I am fairly new at this curl scripting and I am looking for a way to paste a text from a token like value when I hit a specific site.

Currently I have:

echo $a_opensite | grep -E -o 'value=\".*==.*' | awk '{print $1}' > 
                  /Users/MZComputer/Downloads/10Step/VIEWSTATE_file;

which writes the value below in a file called:

VIEWSTATE_file****value="bdC7M0jCKxJNSfHjUVv+4MMt3/ysSLviYkIQnnpntK3qNCarg7H012UIllu+XBDepbWYWktNdf3EBgd3xd0...UVv"****

BUT the text value=" is what I am able to use to get the correct token. However, I do not want to include the value="...." in the file, just want to include the text within the quotation marks - excluding the quotation marks themselves.

Any ideas how I can achieve?

Upvotes: 0

Views: 100

Answers (2)

Wintermute
Wintermute

Reputation: 44023

Assuming GNU grep, I'd use its -P option and Perl-style lookbehind/lookahead expressions. For example:

$ echo 'foo value="something interesting" bar' | grep -Po '(?<=value=\").*?(?=\")'
something interesting

The interesting bits are:

  1. (?=\") -- this is a lookahead expression. It matches the empty string iff it is followed by the specified regex, in this case \". In the example above it'll match the empty string just before " bar at the end of the supplied input.
  2. (?<=value=\") -- this is a lookbehind expression. It matches the empty string iff it is preceded by the specified regex, in this case value=\" In the example above, it'll match the empty string just before something interesting.
  3. .*? matches any sequence of characters non-greedily, which is to say that if the line reads value="stuff" garbage="more stuff" it'll match just stuff and not stuff" garbage="more stuff.

Combined with the -o option, this will isolate the string between value=" and the next ".

Upvotes: 0

Alexandre Fenyo
Alexandre Fenyo

Reputation: 4809

Use the following line:

echo $a_opensite | grep -E -o 'value=\".==.' | awk '{print $1}' | sed 's/value="\([^"]*\)"/\1/' > /Users/MZComputer/Downloads/10Step/VIEWSTATE_file;

Upvotes: 1

Related Questions