grep second instance of same string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep second instance of same string
# 1  
Old 09-28-2011
grep second instance of same string

Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times.

<title>Tonight</title>
<title>Thursday</title>
<title>Friday</title>
<title>Saturday</title>

i was able to find the first occurrence of the line with:
Code:
ls file.xml | while read line; do
    fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`

Which my end result was to output the string "Tonight" all by itself.

However, i am choking on trying to find the 2nd occurrence of <title> and 3rd and so on, so i can print each day of the week as its own variable Smilie Smilie

I am in dire help and cannot figure this out, any help will be greatly appreciated.
# 2  
Old 09-28-2011
How about this:

Code:
SEQ=3
awk -vN=$SEQ -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' infile.xml

# 3  
Old 09-29-2011
that didn't work Smilie

here is the extent of my script so far
Code:
#!/bin/sh

basefolder=/s/batchtmp/weather_tmp

cd $basefolder

ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
SEQ=3
fileTitle2-`awk -vN=$SEQ -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' file.xml`

echo $fileTitle
echo $fileTitle2

done

This is what i get from the terminal
Code:
computer:~ me$ . test.sh 
awk: invalid -v option

Tonight

# 4  
Old 09-29-2011
You may need to use nawk if your on Solaris:

Code:
ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
SEQ=3
fileTitle2=`nawk -vN=$SEQ -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' $line`
 
echo $fileTitle
echo $fileTitle2
 
done

# 5  
Old 09-29-2011
Im on Mac OS X

-bash: nawk: command not found

i guess it doesn't like nawk, ugh
# 6  
Old 09-29-2011
OK, the awk on Mac OS seems a little strange, perhaps best to avoid using -v at all try this:

Code:
ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
SEQ=3
fileTitle2=`awk -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' N=$SEQ $line`
 
echo $fileTitle
echo $fileTitle2
 
done

# 7  
Old 09-30-2011
That worked! Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

One instance of comparing grep and awk

Hi. In thread https://www.unix.com/shell-programming-and-scripting/267833-grouping-counting.html rovf and I had a mini-discussion on grep and awk. Here is a demo script that compares the awk and grep approaches for this single problem: #!/usr/bin/env bash # @(#) s2 Demonstrate group... (1 Reply)
Discussion started by: drl
1 Replies

2. UNIX for Advanced & Expert Users

Grep the only instance name

Hi, I want to get the only application name from the server. Ex: if i give $ ps -ef | grep bw. It will show all BW process with entire path. It will little confuse to list out the process. Can anyone have syntax to get only the instance name. I need this for be, hawk,ems also. Please... (2 Replies)
Discussion started by: ckchelladurai
2 Replies

3. Shell Programming and Scripting

How can I just grep one instance of a word in the file

I want to grep some information out of the dmidecode but when I type dmidecode | grep Memory I get several instances of the word. Is there a way I can just choose which instance I want to display? (8 Replies)
Discussion started by: jcnewton13
8 Replies

4. Shell Programming and Scripting

grep for a string until instance of a space

Hey guys, I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this. I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
Discussion started by: terrell
6 Replies

5. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

6. Shell Programming and Scripting

awk - last instance of a string

How do I use awk to find the last occurence of a string in a file? (3 Replies)
Discussion started by: locoroco
3 Replies

7. Shell Programming and Scripting

awk - find first instance of string after NR==10

How do I use awk to find the NR of first instance of a specific string after eg NR==10? I need to find the first instance of the word "class" after a specific NR. (2 Replies)
Discussion started by: locoroco
2 Replies

8. Shell Programming and Scripting

find the first instance after a string

I have this file (below) and need to get out specific data that appears after OSE1_1.FIX, but before OSE1_2.FIX. Specifically I need to get all of the data after "ROW80_20:", "ROW80_21:", "ROW80_22:" & "ROW80_23:" then I need to do the same for data the appears after OSE1_2.FIX, but before... (2 Replies)
Discussion started by: josslate
2 Replies

9. Shell Programming and Scripting

Search the last instance of a string in a file

I have a big database log file which keepsgrowing daily. OS is HP UX. Here is a small part of it: Tue Jan 27 04:03:22 2009 : add session begin for mfgeb on /dev/pts/th. : Converting relative path to absolute path. : add session end. Tue Jan 27 04:03:29... (6 Replies)
Discussion started by: dinesh1178
6 Replies

10. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies
Login or Register to Ask a Question