grep/sed/awk a value between 2 "


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep/sed/awk a value between 2 "
# 1  
Old 08-14-2009
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
Code:
<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
Code:
c=$(cat somefile)
    for i in $c
    do
        grep someregex > someotherfile
    done

but I'm stuck there.

Last edited by unclecameron; 08-14-2009 at 10:07 PM..
# 2  
Old 08-14-2009
Quote:
Originally Posted by unclecameron
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,
...
Given below are a few techniques for doing this. The redirection part is left for you as an exercise.

Code:
$ 
$ 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
# 3  
Old 08-15-2009
wonderful, thank you very much, I appreciate the help Smilie
# 4  
Old 08-15-2009
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>

Code:
# 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:

Code:
$ 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)


Last edited by Franklin52; 08-15-2009 at 06:39 PM.. Reason: Please use code tags!
# 5  
Old 08-15-2009
jp2542a Smilie

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 [code] and [/code] 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
# 6  
Old 08-16-2009
danmero,

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

Thanks..
# 7  
Old 08-16-2009
Check your last post !
Quote:
Last edited by Franklin52; 6 Hours Ago at 05:39 PM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

2. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

5. Shell Programming and Scripting

Help to change the file with "sed" and "awk"

Hi experts I want your help to change the file format to my wanted version, please give me a hand thanks $cat file install pass make os pass make build kernel failed usb storage pass chane to | *install* | *make os* | *make build kernel* | *usb storage* | | pass | pass... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. Shell Programming and Scripting

grep '\~' b | awk '{print $1","$3}' | sed -e 's/~//g'

Hi all, grep '\~' b | awk '{print $1","$3}' | sed -e 's/~//g' Iam using above command for some report... can this be done using any one of them either sed or awk or grep... (3 Replies)
Discussion started by: harshakusam
3 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question