Search and replaces lines with a variable number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replaces lines with a variable number
# 1  
Old 11-23-2011
Search and replaces lines with a variable number

Helo,

I have a kml file with 39.000 lines.

Need search text and add a sequential number before text.

Original file
Code:
<Placemark>
        <name>POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>2.104510,41.341900</coordinates>
        </Point>
      </Placemark>
      <Placemark>
        <name>POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>1.992010,41.269520</coordinates>
        </Point>
      </Placemark>

final file
Code:
<Placemark>
        <name>1-POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>2.104510,41.341900</coordinates>
        </Point>
      </Placemark>
      <Placemark>
        <name>2-POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>1.992010,41.269520</coordinates>
        </Point>
      </Placemark>

I searched in the forum but have not found the solution... Smilie
Thanks!!!

Last edited by saba01; 11-23-2011 at 07:48 AM..
# 2  
Old 11-23-2011
Code:
 perl -e '$index=1;while(<>){if (/<name>POI<\/name>/){print "<name>".$index++."-POI</name>\n";}else {print;}}' ~/tmp/tmp.dat
<Placemark>
<name>1-POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>2.104510,41.341900</coordinates>
        </Point>
      </Placemark>
      <Placemark>
<name>2-POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>1.992010,41.269520</coordinates>
        </Point>
      </Placemark>

# 3  
Old 11-23-2011
Quote:
Originally Posted by saba01
Helo,

I have a kml file with 39.000 lines.

Need search text and add a sequential number before text.

Original file
Code:
<Placemark>
        <name>POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>2.104510,41.341900</coordinates>
        </Point>
      </Placemark>
      <Placemark>
        <name>POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>1.992010,41.269520</coordinates>
        </Point>
      </Placemark>

final file
Code:
<Placemark>
        <name>1-POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>2.104510,41.341900</coordinates>
        </Point>
      </Placemark>
      <Placemark>
        <name>2-POI</name>
        <styleUrl>#waypoint</styleUrl>
        <Point>
          <coordinates>1.992010,41.269520</coordinates>
        </Point>
      </Placemark>

I searched in the forum but have not found the solution... Smilie
Thanks!!!
Code:
awk '/<Placemark>/{w=1;n++}/<\/Placemark>/{w=0}w{sub("POI",n"-POI")}1' yourkml

regards
ygemici
# 4  
Old 11-23-2011
thanks for the quick response
both solutions work perfectly!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Assign a variable to number of lines in a file

Hello Gurus, Here is my requirement. I need to find the number of lines in a file and need to assign it to a variable. This is what I did and not wroking. #!/bin/ksh set -xv Src_Path=/mac/dev/Generic/SrcFiles Src_Count=wc -l ${Src_Path}/FILE_JUNE.txt Count_file = $Src_Count | awk -F... (2 Replies)
Discussion started by: thummi9090
2 Replies

5. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

6. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

7. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

8. Shell Programming and Scripting

How would I do 2 search & replaces in 1 Perl statement?

Hi All, The below code successfully tails the logfile.log file and colors every word "ERROR" in RED, thanks to the Perl statement below. However, would anyone know how to append 1 additional search/replace to the Perl statement below, to color the word "SUCCESS" in GREEN (using ANSI \e ... (3 Replies)
Discussion started by: chatguy
3 Replies

9. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

10. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies
Login or Register to Ask a Question