Problem using gsub in gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem using gsub in gawk
# 1  
Old 11-06-2012
Question Problem using gsub in gawk

I know that most of you guys probably won't reply to this, because you'll feel like my post doesn't even deserve a glance. However, for those of you that do decide to help- thank you!
Code:
TRAILS TA
2700 E Main St
ALBERT LEA , MN 56007
 507-373-4200

TRUCKER'S INN
State Hwy 30 AT I 35 N
ELLENDALE , MN 56026
 507-688-9433

and I want it to look like this at the end..
Code:
TRAILS T|2700 E Main St|ALBERT LEA|MN|56007|507373420|
TRUCKER'S IN|State Hwy 30 AT I 35 N|ELLENDALE|MN|56026|507688943|

I am obviously a beginner with gawk. I have started out by using simple things like
gsub(/-/, "") but all that it gives me is the numbers, and not any of my other information.
I have no idea what I'm doing, so any help getting me to my eventual goal would be greatly appreciated! Even if it is, "you suck, start out by doing this instead".

Again, thanks!
# 2  
Old 11-06-2012
nice attempt at being subliminal...
Code:
nawk '$1=$1' RS='' OFS='|' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-06-2012
also try:
Code:
awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
{l=l $0 "|";}
' infile

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 11-07-2012
Thank you both so much!
# 5  
Old 11-07-2012
Problem deleting space after area code

Code:
awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
{l=l $0 "|";}
' infile

This works perfectly, except that I need to get rid of gaps in the phone number like
Code:
Speedy Mart #19|US 127 1009 Holmes St|Frankfort|KY|40601|502 8750778|
Marathon 150 Quick Stop|Hwy 150 Exit 25|Bardstown|KY|4004|502 3484930|
Love's Travel Stop #360|Hwy 218 Exit 58|Horse Cave|KY|42749|270 7864000|
Liberty Fuel|US 641 SR 70|Marion|KY|42064|270 9654922|

I've tried adding:
Code:
awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);gsub(([0-9][0-9][0-9]) ([0-9][0-9][0-9][0-9][0-9][0-9][0-9]), "" print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
{l=l $0 "|";}
' infile

It was finding the occurrences with my regular expression, but I don't know how to say "find the space between these numbers, but keep the numbers and delete the space". Any ideas?
# 6  
Old 11-07-2012
try:
Code:
awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
/^ *[0-9 -][0-9 -]* *$/ {gsub("[ -]","")}
{l=l $0 "|";}
' infile

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 11-07-2012
Another one:
Code:
awk '{gsub(/ /,"|",$4); gsub(/[ -]/,x,$5)}NF++' FS=' , |\n' RS='' OFS='|' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk gsub

Hi, I want to print the first column with original value and without any double quotes The output should look like <original column>|<column without quotes> $ cat a.txt "20121023","19301229712","100397" "20121023","19361629712","100778" "20121030A","19361630412","100838"... (3 Replies)
Discussion started by: ysrini
3 Replies

2. Shell Programming and Scripting

GSUB/Regex Help

I am trying to write my gsub regex to replace a bunch of special characters with spaces, so i can split it to an array and look at each word independently. However, my regex skills are slightly lacking and I appear to be missing a quote or something here. I am trying to replace the following... (6 Replies)
Discussion started by: nitrobass24
6 Replies

3. Shell Programming and Scripting

Gawk field separator problem

I'm trying to run a code using gawk and I'm having some trouble with it. What I'm trying to do is pull out value11 from the following input: value11,value12,value13 value21,value22,value23 I have successfully done this before using awk with the following code: awk 'NR == 1 {FS=",";... (4 Replies)
Discussion started by: KomjongShawn
4 Replies

4. Shell Programming and Scripting

How to use gsub and array

Hello, i'm searching for a solution to this problem. I have 2 files, the first one is like: <HTML> <HEAD> <TITLE>{$String1}</TITLE> </HEAD> <BODY> <P>{$String2}</P> </BODY> </HTML>and the other one: {$String1}; french {$String2}; italian {$String3}; english ... {$StringN}; I... (3 Replies)
Discussion started by: heaven25
3 Replies

5. Shell Programming and Scripting

awk gsub simple problem

Hi New to shell script and awk and need assistance on this problem. I need to use a variable to substitute a string in an external file and write the changed info to another file. At first I did not know if you could use a variable as the sub value but the following showed me that I can. ... (3 Replies)
Discussion started by: hukcjv
3 Replies

6. Shell Programming and Scripting

awk gsub

Hi all I want to do a simple substitution in awk but I am getting unexpected output. My function accepts a time and then prints out a validation message if the time is valid. However some times may include a : and i want to strip this out if it exists before i get to the validation. I have shown... (4 Replies)
Discussion started by: pxy2d1
4 Replies

7. Shell Programming and Scripting

Help with AWK and gsub

Hello, I have a variable that displays the following results from a JVM.... 1602100K->1578435K I would like to collect the value of 1578435 which is the value after a garbage collection. I've tried the following command but it looks like I can't get the > to work. Any suggestions as... (4 Replies)
Discussion started by: npolite
4 Replies

8. Shell Programming and Scripting

strange problem with gawk

Hello Freinds, I am facing a starange problem with gawk . My programme is as follows #!/bin/sh gawk -v AMPLITUDE=$1 -v freq=$2 ' \ BEGIN{ for(i=0; i<=2559; i++){ validec = sprintf("%1.3f", AMPLITUDE * sin( (i/2559) * 2* freq * 3.14159 ) ) validec =... (6 Replies)
Discussion started by: user_prady
6 Replies

9. Shell Programming and Scripting

Gsub and nawk

Hello I have problem with reg-expr and function gsub(); File that I want to preprocess look like this: int table ; printf(" variable : ", variable) ; Using nawk I try something like this: for ( .... ) { line = $0 reg_expr = "\.\=]*" "" variable "" "\.\=]*" ; gsub( reg_expr... (1 Reply)
Discussion started by: scotty_123
1 Replies
Login or Register to Ask a Question