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

problem with if/else sub 1

Status
Not open for further replies.

gawkward

Technical User
May 25, 2006
4
US
Any help is greatly appreciated!!

I have an input text file (test_input.txt) that is comma del., and I need to make a substitution in $20 of this file, and write out the entire file after the text is substituted.

I have the following command:

awk -F, '{print $0}' test_input.txt | awk -F, {if ($20 = ".=FOLK:SHPD=.") sub (/=FOLK:SHPD=/, ",", $20) else sub (/" "/, ",", $20) ;} > test_output.txt

1) I want to input the entire file (test_input.txt)
2) if $20 has a ".=FOLK:SHPD=." I want to replace the =FOLK:SHPD= text with a comma (,) and keep the surrounding text as it is. If it doesn't have that text in $20, it should contain " " at which point I want to insert a comma (,).

Any help would be greatly appreciated.

Thanks,
M
 
Hi

[ul]
[li]Do not use the first [tt]awk[/tt] command, only the second ( the one after the pipe )[/li]
[li]Enclose the [tt]awk[/tt] code between single quotes ( ' )[/li]
[li]Use double equal signs ( = ) to test equality or tilde ( ~ ) to match against a regular expression[/li]
[li]Tell it to output the record[/li]
[/ul]
Code:
awk -F, '{if ($20 == ".=FOLK:SHPD=.") sub(/=FOLK:SHPD=/, ",", $20) else sub(/" "/, ",", $20); print}' test_input.txt > test_output.txt
( Not tested, just corrected the most obvious errors. )

Feherke.
 
Feherke,

Thank you so very much for that post, it has given me some valuable tips that I was missing from before. I am still having problems though, and I fear I am not understanding the use of //, and "" in my script.

My input looks like this:

LINE1: +30.21972, -87.84306,4,.09,.09,"G-9",1,4,13," pen=1.780032","LTH",-99,-99,-99,-99,-99,-99," ",-99,"", 0,-99,-99,"5N9/0",-99,-99,-99, -99,"",-99,""

LINE2: +30.21972, -87.84306,4,.09,0,"G-9",1,4,14," pen=1.780032","LTH", 0,100, 0,-99,1,-99," ",-99,"S=FOLK:SHPD=SAND", 0, 0,100,"",-99,-99,-99, -99,"0:0",-99,""

In LINE1, I want to replace the "" (as seen in $20) with a comma ,

In LINE2, I want to replace the =FOLK:SHPD= part of "S=FOLK:SHPD=SAND" with a comma, leaving me with S,SAND

As of right now my code is:

awk -F, '{if ($20 == ".=FOLK:SHPD=.") sub(/=FOLK:SHPD=/, ",", $20) ;
else sub(/""/, ",", $20); print $0}' test_input.txt > test_output.txt

Sorry to drag this out, but I think the symbology within the sub routine may be where I am going wrong.

Thanks in advance.

-Matt
 
Hi

This works for me with [tt]gawk[/tt], also with --traditional option, so I hope will work for you too. ( If not, tell us what kind of [tt]awk[/tt] you use. )
Code:
awk -F, '!sub(/=FOLK:SHPD=/,",",$20){$20="\",\""}1' test_input.txt > test_output.txt

Feherke.
 
Feherke,

Let me start by saying thanks. Your corrections & advice are helping me understand this a little better, and I appreciate that.

I have modified the code:

awk -F, '!sub(/=FOLK:SHPD=/,", ",$20){$20="\", \""}1' test_input.txt | awk '{OFS="," ; print $0}' > test_output.txt

I'm trying to get the output in comma delimitted form, but the above does not seem to work (it comes out space delim).

My output looks like this:

+30.21972 -87.84306 4 .09 0 "G-9" 1 4 14 " pen=1.780032" "LTH" 0 100 0 -99 1 -99 " " -99 "S, SAND" 0 0 100 "" -99 -99 -99 -99 "0:0" -99 ""
+30.21972 -87.84306 4 .3 .3 "G-9" 1 4 15 " pen=1.780032" "LTH" -99 -99 -99 -99 -99 -99 " " -99 ", " 0 -99 -99 "5N9/0" -99 -99 -99 -99 "" -99 ""

But I want this:
+30.21972, -87.84306, 4, .09, 0, "G-9", 1, 4, 14, " pen=1.780032", "LTH", 0, 100, 0, -99, 1, -99, " ", -99, "S, SAND", 0, 0, 100, "", -99, -99, -99, -99, "0:0", -99, "",
+30.21972, -87.84306, 4, .3, .3, "G-9", 1, 4, 15, " pen=1.780032", "LTH", -99, -99, -99, -99, -99, -99, " ", -99, ", ", 0, -99, -99, "5N9/0", -99, -99, -99, -99, "", -99, "",

Again, thanks for the help.

-M
 
Feherke,

Thank you so very much, for solving this problem, but also teaching me some of the basic concepts that were escaping me.

-M
 
Hi

As I see, this was your first question on Tek-Tips. I suggest you to read forum822 ( UNIX Scripting ) too. ( If not already reading. ) There you could learn more about scripting in general but also about [tt]awk[/tt] scripts.

You tried to use pipes. They are very helpful, but will slow down your scripts if are used excessively. Of course imperceptible when working with a few small files, but otherwise could become a pain. The speed difference is even bigger in case of interpreters which compile the script into bytecode.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top