Ana içeriğe atla

Kayıtlar

text processing etiketine sahip yayınlar gösteriliyor

Php urlencode and Curl

I struggle again with curl and html parsing. The VIEWSTATE variables are sometimes hard to deal with. Strange enough, php and curl adds +%0A to the end of an urlencoded variable. And as you might guess this poisons the VIEWSTATE mechanism. A workaround that I used is to trim +%0A at the end of urlencoded strings by substr function.

Bash String Split

I was looking for ways of splitting strings in bash and found and awesome one. If you have a delimited string lets say by an _ you do this first set IFS="_" then ar=($string_delimited_by__) You got an array ar with elements coming from this string.

Rarely known unix text utilities

I have come across with some new text commands in a book that I hadnt seen before .These are : fmt, tac, join,(un)expand,split. Among them I am very impressed by join which makes an Sql-like join between 2 (or more I guess) files which is very useful.Here an example:Suppose you have two files whose one columns are equal (or contains same information, like an id column of sql) and you want to join these files with respect to tht column.You simply write join file1 file2 if the first columns of file1 and file2 are common. If the common column is not first you can define it by -1 clnr or -2 clnr switches. There is also an -o switch which is useful for formatting output.To print the first and second columns from file 1 and say third column from file2 we write -o 1.1,1.2,2.3 .Here is a complete example join file1 file2 -j 3 -o 1.1,1.3,2.2 . Split is another nice command which splits larger files into smaller ones to easily handle them (when uploading,transferring etc). You just write split -...

Text Processing with Awk

Awk is a powerful unix utility. $ awk -F":" '{ print "username: " $1 "\t\tuid:" $3" }' /etc/passwd -F=Field Seperator $1=first field seperated by ":" Awk can use a program which is a file with awk instructions. $ awk -f program.awk inputfile A sample awk program file program.awk is as follows: 1- BEGIN { 2- FS=":" 3- x=0 4- } 5- { print $1 } 6- $1 ~ /regex/ { x++} 7- END { 8- print x 9- } BEGIN is the place for commands to be executed before inputfile is being processed. Here we set FS(field seperator) to : and initiate variable x to 0. Line 5 prints all matching 1st columns. In Line 6 if $1 is similar to /regex/ then variable x is raised by 1. At the END after processing input file variable x is printed to output. FS,RS(Record Seperator),OFS(Output Field Seperator),ORS and NF(Number of Fields) are other special variables in awk. Reference: http://www.ibm.com/developerworks/linux/library/l-awk3.html