Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for string insert an incremental count 2

Status
Not open for further replies.
Jul 10, 2001
5
US
I have a text file that I'm looking for all occurrences of >>Client. I want to then insert a line above it with <Record>1</Record>. The next occurrence would have <Record>2</Record> and so on. I’m not sure if sed would be more appropriate, but I’m not how to do it in either program regardless.

I’ve used ++ in awk which does give me the incremental count, but I don’t know how to reinsert it into source.

Sample Source
--------
>>Client
Data in between
>>Client
Data in between
>>Client
Data in between

Final Output to File
-------
<Record>1</Record>
>>Client
Data in between
<Record>2</Record>
>>Client
Data in between
<Record>3</Record>
>>Client
Data in between

Any help would be much appreciated.
 
Thanks much feherke. Sorry to took so long. Was tek-tips down earlier? I couldn't get in...

Anyway, this is sweet. If you don't mind can you go over what the command is doing?

awk '/>>Client/{print "<Record>"++i"</Record>"}1' /input/file > /output/file

awk is searking for >>Client.
in the print, does the "" tells it to print it as text and not process it as a command?
What does the i do in ++i?
lastly, what is the 1?

Sorry for all the questions, but I don't like to just cut and paste and not understand what I'm doing. Thanks again!
 
Hi

NoobProgrammer said:
Sorry for all the questions, but I don't like to just cut and paste and not understand what I'm doing.
No problem. Understanding the code we use should be the default behavior for all of us. I am glad to see that it is for you.
Code:
awk '
/>>Client/ {      [gray]# if current record matches the expression[/gray]
  [b]print[/b] \         [gray]# print to STDOUT ... *[/gray]
    [i]"<Record>"[/i] \  [gray]# ... a static text ... **[/gray]
    ++i \         [gray]# ... the value of the preincremented variable ... ***[/gray]
    [i]"</Record>"[/i]   [gray]# ... and another static text[/gray]
}
1                 [gray]# always do the default behavior ****[/gray]
' /input/file > /output/file
[gray]*[/gray] - the backslash ( \ ) at the end of the line means the command is continuing in the next line ( note that in my explanation is incorrect, because at the end of line is the comment, not the backslash; but the goal was the explanation )
[gray]**[/gray] - static texts enclosed in double quotes ( " ) can not contain evaluable things, so neither variable references; this if why we have to wrap the static texts around the variable name
[gray]***[/gray] - is similar to the postincrementation : i++ , but if is used in expression the incrementation is performed before the expression's evaluation
[gray]****[/gray] - the [tt]awk[/tt] code is composed by one or more pattern and statements pairs :
Code:
[green]pattern[/green] [blue]{[/blue]      [gray]# condition to execute the statements[/gray]
  [blue]statements[/blue]   [gray]# commands to execute if the pattern is true[/gray]
[blue]}[/blue]
If the pattern is missing, the default is true, which means always execute.
If the statements are missing ( note that it includes the braces ( {} ) too ), the default is to [tt]print[/tt] the current record.

But at least on of the pattern or statements must be present. In the above code I mentioned the pattern as [tt]1[/tt], which means true. And [tt]awk[/tt] executed the default statement. So the following are equivalent :
Code:
[green]1[/green]

[blue]{print}[/blue]

[green]1[/green][blue]{print}[/blue]

Feherke.
 
Regarding comments. I knew that one. :)

/>>Client/ { # Is this always the format to use /pattern/ with { on the same line and no / on this line either?

"</Record>" # Why no / on this line?

1 # Ah....ok.

 
Hi

NoobProgrammer said:
/>>Client/ { # Is this always the format to use /pattern/ with { on the same line and no / on this line either?
That is only matter of style. I wrote it that way because it is closer to my indenting style, the K&R.
NoobProgrammer said:
"</Record>" # Why no / on this line?
You mean backslash ( \ ). That is to "glue" together the pieces of the command cut to separate lines. There is the end of the command, no more next pieces to glue to it.
Code:
/>>Client/ {
  [highlight #fee][b]print[/b] [red]\[/red][/highlight]
    [highlight #fee][i]"<Record>"[/i] [red]\[/red][/highlight]
    [highlight #fee]++i [red]\[/red][/highlight]
    [highlight #fee][i]"</Record>"[/i][/highlight]
}
1

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top