Cleaning AWK code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cleaning AWK code
# 1  
Old 10-06-2012
Cleaning AWK code

Hi

I need some help to clean my code used to get city location.

Code:
wget -q -O - http://www.ip2location.com/ | grep chkRegionCity | awk 'END { print }' | awk -F"[,<]" '{print $4}'

It gives me the city but have a leading space.
I am sure this could all be done by one single AWK

Also if possible to change from " OSLO" to "Oslo"



Example output from
Code:
wget -q -O - http://www.ip2location.com/ | grep chkRegionCity

Reult
Code:
                                <td><input type="checkbox" name="region" id="chkRegionCity" onchange="pickProduct();"></td>
                                <td><label for="chkRegionCity">Region & City</label></td>
                                <td><label for="chkRegionCity">SOR-TRONDELAG, TRONDHEIM</label></td>

Here i like t get
Code:
Trondheim


Last edited by Jotne; 10-06-2012 at 05:10 AM..
# 2  
Old 10-06-2012
Try this awk (I don't like it too much; too much fumbling and fiddling and cheating):
Code:
awk -F"[<>,     ]*" '/label.*RegionCity.*, / {print substr($5,1,1)tolower(substr($5,2))}'

Pipe it from your wget cmd.
# 3  
Old 10-06-2012
used Rudic's last code... to change the case..Smilie

Code:
wget -q -O - http://www.ip2location.com/ | awk -F "[>,<]" '/chkRegionCity/{gsub(" ","",$6);s=$6}END{print substr(s,1,1)tolower(substr(s,2))}'


Last edited by pamu; 10-06-2012 at 06:13 AM.. Reason: added more info.
# 4  
Old 10-06-2012
@RudiC
Code:
awk -F"[<>,     ]*" '/label.*RegionCity.*, / {print substr($5,1,1)tolower(substr($5,2))}'

Does not work correct, since region may contain spaces.
Like
Code:
<td><label for="chkRegionCity">MORE OG ROMSDAL, KRISTIANSUND</label></td>


@pamu
Code:
wget -q -O - http://www.ip2location.com/ | awk -F "[>,<]" '/chkRegionCity/{gsub(" ","",$6);s=$6}END{print substr(s,1,1)tolower(substr(s,2))}'

Works fine
# 5  
Old 10-06-2012
OK, remove the space & tab from the FS definition and use $6 instead of $5:
Code:
awk -F"[<>,]" '/label.*RegionCity.*, / {print substr($6,2,1)tolower(substr($6,3))}' file
Kristiansund

# 6  
Old 10-06-2012
Now it works Smilie
# 7  
Old 10-06-2012
i am not able to understand the field seperator here Smilie

can someone please explain what this means
Code:
"[<>,]"

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cleaning through perl or awk a Stemmer dictionary

Hello, I work under Windows Vista and I am compiling an open-source stemmer dictionary for English and eventually for other Indian languages. The Engine which I have written has spewed out all lemmatised/expanded forms of the words: Nouns, Adjectives, Adverbs etc. Each set of expanded forms is... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Cleaning output using awk

I have some small problem with my code. data.html <TD class="statuscol2">c</TD> <TD class="statuscol3">18</TD> <TD class="statuscol4"><SPAN TITLE="#04">test4</SPAN></TD> <TD... (4 Replies)
Discussion started by: Jotne
4 Replies

3. Shell Programming and Scripting

Difficulty cleaning references to duplicated images in HTML code

Hi, I need to search and replace references to duplicated images in HTML code. There are several groups of duplicated images, which are visually the same, but with different filenames. I managed to find the duplicated files themselves, but now I need to clean the code too. I have a CSV file with... (9 Replies)
Discussion started by: mdart
9 Replies

4. Shell Programming and Scripting

cleaning the file

Hi, I have a file with multiple rows. each row has 8 columns. Column 8 has entries separated by commas. I want to exclude all the rows in which column 8 has more than 3 commas. 1234#0/1 - ABC_1234 3 ATGCATGCATGC HHHIIIGIHVF 1 49:T>C,60:T>C,78:C>A,76:G>T,65:T>G Thanks, Diya (3 Replies)
Discussion started by: Diya123
3 Replies

5. Shell Programming and Scripting

File cleaning

HI , I am getting the source data as below. Source Data CDR_Data,,,,, F1,F2,F3,F4,F5,F6 5,5,6,7,8,7 6,6,g,,, 7,7,76,,, 8,8,gt,,, 9,9,df ,d,d,d ,,,,, (4 Replies)
Discussion started by: wangkc
4 Replies

6. UNIX for Dummies Questions & Answers

AWK Data Cleaning

Hello, I am trying to analyze data I recently ran, and the only way to efficiently clean up the data is by using an awk file. I am very new to awk and am having great difficulty with it. In $8 and $9, for example, I am trying to delete numbers that contain 1. I cannot find any tutorials that... (20 Replies)
Discussion started by: carmar87
20 Replies

7. Shell Programming and Scripting

SED help - cleaning up code, extra spaces won't go away

Hello, W/in the script I'm working on, I have a need to take a column from a file, and format it so I can have a variable that will egrep for & invert the regex from another file. My solution is this: VAR=`awk -F, '{print $2}' $FAIL | sed 's/-i/\|/g'` VAR2=`echo $VAR | sed 's/... (5 Replies)
Discussion started by: Matthias03
5 Replies

8. AIX

doing some spring cleaning....

USERS="me you jim joe sue" for user in ${USERS}; do rmuser -p $user usrdir=`cat /etc/passwd|grep $user|awk -F":" '{ print $6 }'` rm -fr `cat /etc/passwd|grep $user|awk -F":" '{ print $6 }'` echo Deleting: $user '\t' REMOVING: $usrdir done This is for AIX ONLY!!! but easily ported to... (0 Replies)
Discussion started by: Optimus_P
0 Replies
Login or Register to Ask a Question