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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using $ in search string 1

Status
Not open for further replies.

wgmallard

Programmer
Sep 6, 2012
2
0
0
US
I have a file format that has blank lines within the record, and uses $$$$ as a record end.
Thus \n$$$$\n is the end of the record, the the next character starts the next record.
I have made several attempts to parse this with awk,
RS = "\n$$$$\n" does not work
RS = /\n\$\$\$\$\n/ does not work
and several other options do not work.
Now the thing that makes this strange is that if I go into the file and do an edit
$$$$ -> ####, i.e. changing the 4 $ to 4 #, the following value for RS works perfectly:
RS="\n####\n"

It would appear that the fact that the $ is the end of match marker is getting in the way here, but it would seem that it should be escaped with the \$ and the character just looked at as $, but it is not.
In one sense I have solved the problem by editing the file first and then processing it, but I would like to know if anyone has an idea of what is going on and what RS value I should be using to get the desired result.
Thanks.
Gary
 
Hi

Code:
awk -vRS='\n\\$\\$\\$\\$\n' 1 /input/file

[gray]# or[/gray]

awk 'BEGIN{RS="\n\\$\\$\\$\\$\n"}1' /input/file
You assign a string to [tt]RS[/tt], which will be interpreted as regular expression. In regular expression you have to escape the literal dollar sign ( $ ) with a backslash ( \ ). In string you have to escape regular backslash with a backslash.

Feherke.
[link feherke.github.com/][/url]
 
Feherke,
Thanks for the solution, it worked perfectly and more importantly thank you for the discussion of what the issue in the problem was. I had not been aware that the RS string was interpreted as a regular expression when given as a string. I had even tried RS = /\n\$\$\$\$\n/ with no luck - which I would have thought was the same, but it to did not work, but probably would have had it been double escaped. I should note that this is being done in an awk script since subsequent processing is more complex, but this was the key.
Thanks again.
Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top