Find and increment at each occurence of string (loop)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and increment at each occurence of string (loop)
# 1  
Old 02-19-2015
Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with.
Code:
VLCSTART='<vlc:id>'
VLCV=0
VLCEND='</vlc:id>'

Code:
echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCEND

Output file contains several occurences of this:
Code:
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="http://www.videolan.org/vlc/playlist/0">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>

I need to create loop to increment each occurrence of '0' +1 in string '<vlc:id>0</vlc:id>' but the first occurrence has to stay '0'.

I am not good at loops, still learning.... Smilie
# 2  
Old 02-19-2015
Hello TiedCone,

Following may help you in same, I have created a custom file with some more input as follows.
Code:
cat testtest123
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>

Then following code may help.
Code:
awk 'BEGIN{B=0} /<vlc:id>/{A=$0;sub(/>.*/,">",$0);sub(/.*</,"<",A);print $0 B A;B++;next} 1' testtest123

Output will be as follows.
Code:
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>1</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>2</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>3</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-19-2015
You are fast mate. Thanks, perfect solution. Smilie I have another one...

Assume tha last occurrence of <vlc:id>50</vlc:id> has value 50 I need to create the same amount of lines:
Code:
            <vlc:item tid="0"/>
            <vlc:item tid="1"/>
            <vlc:item tid="2"/>
...
            <vlc:item tid="50"/>

Smilie
# 4  
Old 02-19-2015
Since it is a loop, and assuming $VLCV has been set to 0 outside the loop, just append after the echo -e command
Code:
VLCV=$(( $VLCV + 1 ))

hth
# 5  
Old 02-19-2015
Quote:
Originally Posted by TiedCone
You are fast mate. Thanks, perfect solution. Smilie I have another one...

Assume tha last occurrence of <vlc:id>50</vlc:id> has value 50 I need to create the same amount of lines:
Code:
            <vlc:item tid="0"/>
            <vlc:item tid="1"/>
            <vlc:item tid="2"/>
...
            <vlc:item tid="50"/>

Smilie
Hello TiedCone,

Following may help you in same.
Code:
awk -vLINES=`cat Input_file | wc -l` 'BEGIN{B=0;C=0} /<vlc:id>/{A=$0;sub(/>.*/,">",$0);sub(/.*</,"<",A);print $0 B A;B++;if(LINES/8==B){while(C<=B-1){print $0 C A;C++}};next } 1'  Input_file

Output will be as follows.
Code:
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>1</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>2</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
        <track>
            <location>http://ts://</location>
            <title>name</title>
            <extension application="VideoLAN - 404 not found">
                <vlc:id>3</vlc:id>
                <vlc:id>0</vlc:id>
                <vlc:id>1</vlc:id>
                <vlc:id>2</vlc:id>
                <vlc:id>3</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>

NOTE: Here I am considering that your Input_file will have 8 lines of set means it will always start from tag <track>
and end with tag </track> within 8 lines in it each tag.


Thanks,
R. Singh
# 6  
Old 02-19-2015
@sea: That was my first thought as well, but the requestor uses sh, so VLCV=$(expr $VLCV + 1) might be more suitable.

@TiedCone: Please open a new thread for a new request. Where do you get the 50 from?
# 7  
Old 02-19-2015
This is not what I want to achieve. Smilie
Lines:
Code:
<vlc:item tid="0"/>

are outside of <track></track>.

This is completely different string but its values have to match values of
Code:
<vlc:id></vlc:id>

so the output will look like this:
Code:
        <track>
            <location>http://ts:/</location>
            <title>name</title>
            <extension application="http://www.videolan.org/vlc/playlist/0">
                <vlc:id>0</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
...
        <track>
            <location>http://ts:/</location>
            <title>name</title>
            <extension application="http://www.videolan.org/vlc/playlist/0">
                <vlc:id>50</vlc:id>
                <vlc:option>network-caching=1000</vlc:option>
            </extension>
        </track>
    </trackList>
    <extension application="http://www.videolan.org/vlc/playlist/0">
            <vlc:item tid="0"/>
            <vlc:item tid="1"/>
            <vlc:item tid="2"/>
            ...
            <vlc:item tid="50"/>

So need code for generating these lines:
Code:
<vlc:item tid="0"/>

@RudiC
'50' is the number of last occurrence of <vlc:id>0</vlc:id> in file and is flexible depends on input data file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and increment value in string of hex

I have a long string of hex (from ASN.1 data) where I need to find and change a particular hex value only and increment it. The hex pairs either side (84 and a7) of the value to increment will remain constant. i.e. "84 <length> <value_to_increment> a7" starting with 00. So end result: ... (11 Replies)
Discussion started by: securegooner
11 Replies

2. Shell Programming and Scripting

Find the occurence of particular string in log file

I have a log file which looks like this: <845185415165:STATUS:5/0:0:0:0:0|ghy59DI5zasldf87asdfamas8df9asd903tGUVSQx4GJVSQ==> I have to extract DATE and number of times the keyword STATUS is shown on each date. Input is : <1354625655744:STATUS:5/0:0:0:0:0|ghy59DI5ztGUVSQx4GJVSQ==>... (8 Replies)
Discussion started by: maddyrox
8 Replies

3. Programming

[Xquery] How to do a increment in a For loop

Hello men. How can i build a simple increment for $a by Xquery such as ? let $a := 0 for $i in (1 to 10) let $a := $a + 1 return $a why a in this loop always is '1' Thank you for reading, its will really helpful for my job if i can solve this problem :D:D (3 Replies)
Discussion started by: tien86
3 Replies

4. UNIX for Dummies Questions & Answers

To find the Nth Occurence of Search String

Hi guys, I like to find the Line number of Nth Occurence of a Search string in a file. If possible, if it will land the cursor to that particualar line will be great. Cheers!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Shell Programming and Scripting

shell script to find the second occurence of the alphabet in a string

this is my assignment question. i'm supposed to submit it tommorow. can somebody please help me with it? Do not post homework questions in the main forums. Please post in the homework forum using the correct template. (0 Replies)
Discussion started by: vijjy
0 Replies

6. Shell Programming and Scripting

find a string in a loop

Hi, Can anyone help with the problem below? I need to read all text files (file1, file2, file3) in a loop and find file1.txt: file2.txt: File3.txt: (7 Replies)
Discussion started by: Lenora2009
7 Replies

7. Shell Programming and Scripting

Find index of last occurence of a character within a string

I need to find the index of last '|' (highlighted in bold) in awk : |ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if Please suggest a way... Thanks (5 Replies)
Discussion started by: joyan321
5 Replies

8. Shell Programming and Scripting

Increment date in 'for' loop?

Hi Guys, My first post..:) Right...I want to move existing files (with some date in their name) currently in $mainftp, to $mainfolder/$foldate/system1. I'd like to be able to increment date in the for loop? Is this possible or should I use a different technique. The script return the... (4 Replies)
Discussion started by: SunnyK
4 Replies

9. Shell Programming and Scripting

How to find vowel's occurence in a string

Hi All, I want to search the string for vowel's occurence and find the no of occurence of each vowels, Could anyone help me out? This is urgent to me...I m new to Shell programming.. Thanks and Regards, Nids:b: (4 Replies)
Discussion started by: Nidhi2177
4 Replies

10. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 Replies
Login or Register to Ask a Question