grep/sed/awk a value between 2 "

I have a log file where I'm trying to grep/awk/sed the value between the set of quotes following VALUE="somevalue" in somefile and > it to someotherfile, so take a line like

<NODE NAME="OS Version" VALUE="Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)" TR="N=4102" />

and output

Microsoft Windows XP...

so I'm trying to read in each line in the file like this

c=$(cat somefile)
    for i in $c
    do
        grep someregex > someotherfile
    done

but I'm stuck there.

Given below are a few techniques for doing this. The redirection part is left for you as an exercise.

$ 
$ cat -n f1
     1    this is line # 1
     2    <NODE NAME="OS Version" VALUE="Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)" TR="N=4102" />
     3    this is line # 3
$ 
$ # 1
$ grep VALUE= f1 | cut -d'"' -f4
Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)
$ 
$ # 2
$ sed -n 's/.*VALUE="\([^"]*\)".*/\1/p' f1
Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)
$ 
$ # 3
$ awk -F'"' '/VALUE=/{print $4}' f1
Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)
$ 
$ # 4
$ perl -lne '/.*VALUE="(.*?)".*/ && print $1' f1
Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)
$ 

HTH,
tyler_durden

wonderful, thank you very much, I appreciate the help :slight_smile:

This is a version of an awk clause that will work will any version of awk. It will also ignore lines in the file that don't have a VALUE= in it. And doesn't care where the VALUE= appears in the line. Usage: awk -f <file-with-awk-clause> <input-file>

# begin awk clause
# only look at input if it has a value k

/VALUE=/{
# get position of Value key
        i=index($0,"VALUE")
# get the string from index to end
        v = substr($0, i+6)
# now get the value using split
        p = split(v, a, "\"")
# print it (index 1 is a empty string)
        print a[2]
}

# End awk clause

Example using cygwin:

$ cat v.test
line 1
<NODE NAME="OS Version" VALUE="Microsoft Windows XP Professional Service Pack 2
(build 2600) (5.1.2600)" TR="N=4102" />
<NODE NAME="OS Version" VALUE="Microsoft Windows yP Professional Service Pack 2
(build 2600) (5.1.2600)" TR="N=4102" />
line5
<notheig>

$ awk -f value.awk v.test
Microsoft Windows XP Professional Service Pack 2 (build 2600) (5.1.2600)
Microsoft Windows yP Professional Service Pack 2 (build 2600) (5.1.2600)

jp2542a :mad:

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

danmero,

I not understanding where I went wrong with my post.. please explain the specific error so I don't do it again.

Thanks..

Check your last post !