awk

Extract IP from netstat output

The netstat output contains thing like... tcp 0 0 0.0.0.0:80 221.126.149.99:51973 ESTABLISHED 23879/apache2 tcp 0 0 0.0.0.0:80 66.249.68.154:40883 ESTABLISHED 23899/apache2 tcp 0 0 0.0.0.0:80 66.249.68.81:41200 ESTABLISHED 23892/apache2 tcp 0 0 0.0.0.0:80 ...

Printing lines from a file where a specific field is matched

Hi, I have many lines of form: A:B:C I want to print those lines(complete) where the 3rd field (fields separated by :) contain a certain pattern. Example: new/old:california/new york:/ms/dist/fx/PROJ/fx/startScript new/old:startScript/new york:/ms/dist/fx/PROJ/fx/stopScript When searching for pattern startScript, the 1st line shoul...

How to print this using AWK?

I have a file that look like this: 1 543423 34354 2 5654656 3423 xyz_1378,xyz_1379 3 4645656 34234354 xyz_1384,xyz_1385 4 5654 78678 xyz_1390,xyz_1391,xyz_1392 5 54654 76867 xyz_1411,xyz_1412,xyz_1413 6 54654 8678 7 56546 67867 xyz_1711 8 678 7867 9 76867 7876 xyz_2940 10 6786 678678 xyz_3101,xyz_3102,xyz_3103,xyz_3104,xyz_3105,xyz_3...

Printing lines from a file where a specific field does not start with something..

Hi, I want to print all the lines where 3rd field (fields separated by : ) DO NOT start with # (to signify that 3rd field is a comment). Please note that there may be space(s) between : and #. Example Input: A:B:#hdfghdfg A:B: #dfdfdfg A:B:C Desired output: A:B:C I tried: awk -F : '$3 ~ /^#/ { print }' run_out5 > run_out6 b...

How do I preserve whitespace and use a ceiling function with a One-Liner Awk inline edit?

Hi everyone, I have a configuration file that has variables and value separate by spaces. I want to take the value (in the second column) of certain matches and inline edit the file, dividing this match by 2. I also would like to preserve spacing and use a ceiling function on the value. For example, the file: To recap, using an actu...

How do you Include Short Functions in Awk One-Liners/Single Commands?

I know that usually you don't want one-liners/single commands to get too long but it seems like there's occasionally a longish one-liner that would benefit from replacing repetitive elements with a function. Is it possible to use a short function chop down the length of your command? For example there's no ceiling or round function ...

How to write find-all function (with regex) in awk or sed

I have bash function which run python (which return all finded regex from stdin) function find-all() { python -c "import re import sys print '\n'.join(re.findall('$1', sys.stdin.read()))" } When I use this regex find-all 'href="([^"]*)"' < index.html it should return first group from the regex (value of href attribute from file in...

Is there a cleaner way to add "else if" to assignment conditional in Awk, etc.?

Certain languages like awk script allow for conditional assignments. For example, say you had a list file in the format: <item name, no spaces> <price as float> e.g. Grape 4.99 JuicyFruitGum 0.45 Candles 5.99 And you wanted to tax everything over $1... you could use the awk script: awk '{a=($2>1.00)?$2*1.06:$2; print a}' prices.d...

exit out of an awk function

How do I exit out of an awk function? "exit" stops entire program? ...

How to convert a text file into csv using AWK

I am collecting usb usage details of all users and convert it into CSV files so that I can export it into some database. The input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB Device instanceID: 0018F3D974B4A9C0E1760896&0 ParentIdPrefix: 7&b62e00e&2 Last Mounted As: \DosDevices\I:...

Need command line expansion to print all keyboard characters

Is there an awk-like or sed-like command line hack I can issue to generate a list of all keyboard characters (such as a-zA-z0-9!-*, etc)? I'm writing a simple Caesar cipher program in my intro programming class where we do the rotation not through ASCII values, but indexing into an alphabet string, something like this: String alphabet ...

how to display all lines from one that match regex in linux.

I want to display all lines from one which match regular expression if I have a file foo bar123 baz12435 lorem ipsum dolor sit amet this display-from baz[0-9]* < file sould return (It doesn't matter if it display matched line or not) lorem ipsum dolor sit amet How can I do this in Linux (with sed, awk or grep) ...

Using awk create two arrays from two column values, find difference and sum differences, and output data

I have a file with the following fields (and an example value to the right): hg18.ensGene.bin 0 hg18.ensGene.name ENST00000371026 hg18.ensGene.chrom chr1 hg18.ensGene.strand - hg18.ensGene.txStart 67051161 hg18.ensGene.txEnd 67163158 hg18.ensGene.exonStarts 67051161,67060631,67065090,67066082,67071855,67072261,67073896,67075980,67078739...

get list of sections from ini-file using bash (sed/awk)

I want to create a var from the section names of an ini file like: [foo] ; ... [bar] ; ... [baz:bar] ;... now I need a var like SECTIONS="foo bar baz" thanks in advance ...

Using awk printf to urldecode text

Edited to correct the question I'm using awk to urldecode some text. If I code the string into the printf statement like so: printf "%s", "\x3D" it correctly outputs =. The same if I have the whole escaped string as a variable. However, if I only have the 3D, how can I append the \x so printf will print the = and not \x3D. I'm using ...

Other solutions/languages that are superior to the TCL-based Expect?

I am amazed by how Expect (TCL) can automate a lot of things I normally could not do. I thought I could dig deeper into Expect by reading a book, but before I do that I want to ask if there are other solutions/languages that could do what Expect does? Eg. I have read that people compare Expect with Awk and also Perl. Could Awk and Per...

How to sort specific values from an xml file

Hi, all! I have an xml file as follows: <Exchange DateTime="17/09/2010 18:00:00"> <Content Percent="0.20" Price="63.862" Sign="1"/> <Content Percent="0.25" Price="80.989" Sign="1"/> <Content Percent="0.07" Price="1.4970" Sign="1"/> <Content Percent="-0.31" Price="1.9530" Sign="-1"/> </Exchange> There are 4 variables called A,B,C,D. ...

Writing an Awk Script

How to write a simple Awk script to parse the output of the ps command ...

using grep or awk to find files that contain both "a" and "b" that are on different lines

Is there a way to use grep or awk to find text files that contain both e.g. "a" and "b" but in this case "a" and "b" are on different lines? ...

Regarding UNIX Variable Date Format

Hi, I have a date value stored in a unix korn shell variable VALUE="2010_09_23" I want to change it to "23-Sep-2010" and store it in VALUE1. How can i do it? VALUE1="23-Sep-2010" ...