awk

Awk array iteration for multi-dimensional arrays

Awk offers associative indexing for array processing. Elements of 1 dimensional array can be iterated: e.g. for(index in arr1) print "arr1[" index "]=" arr1[index] But how this kind done for a two dimensional array? Does kind of syntax,given below work? for(index1 in arr2) for(index2 in arr2) arr2[index1,index2] ...

Change Number Format

I have a lot lines contains XXXXXXXXX-XXX.XXX number format. I want change number XXXXXXXXX-XXX.XXX to XX.XXX.XXX.X-XXX.XXX XXXXXXXXX-XXX.XXX = 15 digit random number Anyone can help me? Thanks in advance ...

awk + print the second field in line only if the four field is 0

How to print $2 by awk only if the fourth field is not 0 (zero). line="root 13246 11314 457 15: qsRw -m1" then awk will print 13246, but if line="root 13246 11314 0 15: qsRw -m1" then awk will not print anything ...

Shell scripting - running a regex to modify a file in place, that includes the file name.

I have many html files named 12345.html, 12346.html, etc. I need to change "style.css" to "style-12345.css" or the appropriate file name. I'm not sure what tool to use, recommendations? ...

Print just the last line of a file?

How to print just the last line of a file? ...

matching columns from two files

Dear all, I have two files, in which the first column of them might have same values. I would like to match the first column of both files, and print the lines in FILE1 for which there was a match. FILE1: xxx1 yyy yyy yyy xxx2 yyy yyy yyy xxx3 yyy yyy yyy FILE2: xxx3 zzzz xxx4 zzzz OUTPUT: xxx3 yyy yyy yyy Any suggestions are welco...

awk / gawk asorti() problem

Hello stack. I've got following problem with gawk's asorti function: gawk 'BEGIN{ \ a[1]=6; \ a[2]=7; \ a[3]=8; \ a[21]=9; \ a[123]=10; \ t=asorti(a, o); \ for (i=1; i<=t; i++) { \ print i,o[i]; \ } \ }' The result is: 1 1 2 123 3 2 4 21 5 3 So it's pretty clear awk, sorted indices in lexicographi...

How To Sort Tab Format File Based on Length of Column K

I have a space delimited tabular file that looks like this: >NODE 28 length 23 cov 11.043478 ACATCCCGTTACGGTGAGCCGAAAGACCTTATGTATTTTGTGG >NODE 32 length 21 cov 13.857142 ACAGATGTCATGAAGAGGGCATAGGCGTTATCCTTGACTGG >NODE 33 length 28 cov 14.035714 TAGGCGTTATCCTTGACTGGGTTCCTGCCCACTTCCCGAAGGACGCAC How can I use Unix sort to sort it by leng...

How to make AWK use the variable created in Bash Script

I have script that looks like this #!/bin/bash #exampel inputfile is "myfile.txt" inputfile=$1 basen=`basename $inputfile .txt` # create basename cat $inputfile | awk '{print $basen "\t" $3} # this doesn't print "myfile" but the whole content of it. What I want to do above is to print out in AWK the variable called 'basen' created...

Remove all the text using sed

Format: [Headword]{}"UC(icl>restriction)"(Attributes);(gloss) The testme.txt file has 2 lines [testme] {} "acetify" (V,lnk,CJNCT,AJ-V,VINT,VOO,VOO-CHNG,TMP,Vo) <H,0,0>; [newtest] {} "acid-fast" (ADJ,DES,QUAL,TTSM) <H,0,0>; The expected output is this: testme = acetify newtest = acid-fast What I have achieved so far is: cat te...

Setting a BASH environment variable directly in AWK (in an AWK one-liner)

I have a file that has two columns of floating point values. I also have a C program that takes a floating point value as input and returns another floating point value as output. What I'd like to do is the following: for each row in the original, execute the C program with the value in the first column as input, and then print out the...

How can i reorder a file by ascending order (column)?

I would like to reorder a whole file by ascending time order. file.txt looks like this: a 12.24 text a 1.45 text b 5.12 text i would like it to look like this a 1.45 text b 5.12 text a 12.24 text Thanks in advance ...

Sed to remove underscores and promote character

Hi, I am trying to migrate some code from an old naming scheme to the new one the old naming scheme is: int some_var_name; New one is int someVarName_: So what I would ilke is some form of sed / regexy goodness to ease the process. So fundamentally what needs to happen is: find lower case word with contained _ replace underscore w...

delete whitespace at the end of number format

Hi.. I have several line contains number like: XXXXXX.XXX (this number ended by whitespace.) How can I delete whitespace at the end of number format using sed? Thank for the help. ...

can awk deal with csv file that contain comma inside a quote

I am using awk to perform counting the sum of one column in the csv file. The data format is something like: id, name, value 1, foo, 17 2, bar, 76 3, "I am the, question", 99 I was using this awk script to count the sum: awk -F, '{sum+=$3} END {print sum}' Some of the value in name field contains comma and this break my awk script....

Reorganizing named fields with AWK

I have to deal with various input files with a number of fields, arbitrarily arranged, but all consistently named and labelled with a header line. These files need to be reformatted such that all the desired fields are in a particular order, with irrelevant fields stripped and missing fields accounted for. I was hoping to use AWK to ha...

Looking for powershell equivalent of specific awk command...

My google-fu's failing me again. The information is (probably) out there, but I can't find it. I know UNIX like the back of my hand, use cygwin etc. however with the increased availability of Powershell on servers, and (on production servers at least) the difficulty at getting cygwin in place, I'm attempting to pick up Powershell. If not...

awk + remove all not uniq lines except the first line uniq (FILE NAME)

hi I have the following file How to remove by sed all FILE NAME lines except the first uniq FILE NAME For example need to remove all FILE NAME lines from the file except the first: FILE NAME: /dir1/dir2/dir3/dir4/dir5/file FILE NAME: /dirA/dirB/dirC/dirD/dirE/file the file: FILE NAME: /dir1/dir2/dir3/dir4/dir5/file PARAMETER NAME: bl...

how to pre-construct awk statement to pass to awk on command line?

I have a shell script that constructs an awk program as a string then pass that string to awk. This is because I want to use values of shell variables in the awk program. My code looks like this: awk_prog="'{if (\$4~/$shell_var/) print \$1,\$2}'" echo $awk_prog awk $awk_prog $FILENAME However, when I pass the string to awk, I always...

changing content of the column without destroying formating

Hi, $ echo "a b" | awk '{print $0; $1="1"; print $0}' a b 1 b I would like to receive formatted output like this: a b 1 b Is there easy way to do that (without IFS,OFS changes)? I am changing columns in big table and afterwards it looks ugly. I don't want to reformat each column. Thanks. ...