awk

How to write a unix filter that outputs only a line every N lines

Suppose to feed the filter standard input with these line: line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 It would be nicer if someone tell me how to write a script that prints only every 4 lines, in the case of the example input above: line 1 line 5 line 9 ...

What are the differences between Perl, Python, AWK and sed?

Hello, just want to know what are the main differences among them? and the power of each language (where it's better to use it). Edit: it's not "vs." like topic, just information. ...

Awk question: combining fields

Hi, is there an easy away to combine fields in awk? Specifically, I have lines like this: 1,2,some text 3,4,some text, but it has commas in it, annoyingly I want to extract the two numbers as the first two fields, but then I want all of the text (commas and all) to be the third field. Is there a way to do that? My basic awk call was...

[bash] Fill placeholders in file in single pass

I have a skeleton text file with placeholder strings: blah blah blah blah $PLACEHOLDER_1$ blah $PLACEHOLDER_2$ and so on. Specific "form" of placeholders does not matter -- I may change them to whatever most comfortable for specific implementation. I have a bash script where I know values for placeholders, and I need to generate a ne...

Keeping original format POST passing through AWK

I have an issue with using AWK to simply remove a field from a stream, illustrated below: 1 int blah (void) 2 { 3 if (foo) { 4 printf ("blah\n"); 5 } 6 return 0; 7 } I use the following code to remove the first field: $ awk '{ $1=""; print }' example.out int blah (void) ...

Remove whitespace before a field using AWK

(Almost exact duplicate of http://stackoverflow.com/questions/408469/keeping-original-format-post-passing-through-awk submitted by same person.) I have a simple question pertaining to gawk, illustrated below: 1 int blah (void) 2 { 3 if (foo) { 4 printf ("blah\n"); 5 } 6 return ...

How can I use awk or Perl to increment a number in a large XML file?

I have an XML file with the following line: <VALUE DECIMAL_VALUE="0.2725" UNIT_TYPE="percent"/> I would like to increment this value by .04 and keep the format of the XML in place. I know this is possible with a Perl or awk script, but I am having difficulty with the expressions to isolate the number. ...

Linux command line global search and replace

I'm trying to search and replace a string in all files matched by grep on a linux machine. I've got some pieces of what I want to do, but I'm unsure how best to string them all together. grep -n 'foo' * will give me output in the form: [filename]:[line number]:[text] For each file returned by grep, I'd like replace "foo" with "bar" ...

sed/awk: DOS to UNIX path substitution within a file

I have a file that contains this kind of paths: C:\bad\foo.c C:\good\foo.c C:\good\bar\foo.c C:\good\bar\[variable subdir count]\foo.c And I would like to get the following file: C:\bad\foo.c C:/good/foo.c C:/good/bar/foo.c C:/good/bar/[variable subdir count]/foo.c Note that the non matching path should not be modified. I know how...

Awk appears to disconnect my DB2 session when piping

Hello. I'm attempting to run the following command in Korn Shell (ksh): set -A INDEXES `db2 "describe indexes for table ${TABSCHEMA}.${TABNAME} show detail" | awk '{print $1"."$2}'` What I'm attempting to achieve is place a list of the indexes over a particular table into an array which I can later iterate through. The problem is, w...

Search and replace in Shell

I am writing a shell (bash) script and I'm trying to figure out an easy way to accomplish a simple task. I have some string in a variable. I don't know if this is relevant, but it can contain spaces, newlines, because actually this string is the content of a whole text file. I want to replace the last occurence of a certain substring w...

Where are the simple AWK examples to get started?

All of my codes fail. They should print "he", "hello" and "5 \n 3", respectively: awk -v e='he' {print $e} // not working, why? awk NF { print hello } awk { print ARGV[5,3] } Are there some simple examples about AWK? ...

How can AWK do array arithmetics?

I want to shift away from Excel to Awk. I need basic mathematical operations, such as addition and division, for arrays. For example, arrays A and B are [1, 3, 2] and [2, 1, 2], respectively. How can I get an array [2, 3, 4] from the multiplication between A and B? What about the addition and division between A and B? ...

What is the easiest way to join 12 columns?

I have 12 columns separated by a tab. How can I join them side-by-side? [Added] You can also tell me other methods as AWK: the faster the better. ...

does awk support quantifiers - i want [0-9]{6,6}

gawk doesn't seem to match six digit fields - or n digit fields using the {n,m} quantifiers It does match [0-9][0-9][0-9][0-9][0-9][0-9] ok. Doesn't seem to support \d\d\d\d\d\d either. Do i need to turn on extended reg ex, or does it just not support that. Tnx ...

ASSIGN win XP commandline output to variable

i would like to translate a following script from linux shell to Windows XP shell GPSID=$(awk '/GPSID/ {print $3}' gora.RTK ) awk -v variable=${GPSID} 'BEGIN {printf "Numer seryjny : " variable,$1}' >>out.txt The second line has been translated; the problem is with defining a variable that contains shell output in windows :-( ...

Substract between columns and print it with AWK

Hi, I have a following input data: AATGCCACGTTGAGAGGCGTTCGCGGAAGGCGCG 44 40.000 AATGCCACTCGGGTCCGAGATGGCGGATCTCCAC 35 37.000 AATGCCAGAGCTGTGGTAGTGTGAAAGCAGCAGT 13 13.000 What I want to do is to substract column 3 by column 2 yielding: AATGCCACGTTGAGAGGCGTTCGCGGAAGGCGCG -4 AATGCCACTCGGGTCCGAGATGGCG...

AWK - My regexp won't respect case.

I'm running Ubuntu 8.04 and my code looks like this... for (i=1;i<=n;i++) { if (arr[i] ~ /^[A-Z]{2,4}$/) printf(arr[i]) } I quickly discovered that the {n} expression won't work in gawk without the --posix switch. Once enabled the expression works but it is case-insenitive matching AAAA and aaaa. What is going on here? ...

Parsing each field and process it using 'awk'/'gawk'

Hi all, Here is a query: grep bar 'foo.txt' | awk '{print $3}' The field name emitted by the 'awk' query are mangled C++ symbol names. I want to pass each to dem and finally output the output of 'dem'- i.e the demangled symbols. Assume that the field separator is a ' ' (space). ...

How to compute the p-value in hypothesis testing (linear regression)

Currently I'm working on an awk script to do some statistical analysis on measurement data. I'm using linear regression to get parameter estimates, standard errors etc. and would also like to compute the p-value for a null-hypothesis test (t-test). This is my script so far, any idea how to compute the p-value? BEGIN { ybar = 0.0 ...