awk

Ignoring escaped delimiters (commas) with awk?

If I had a string with escaped commas like so: a,b,{c\,d\,e},f,g How might I use awk to parse that into the following items? a b {c\,d\,e} f g ...

How can I selectively awk a string?

If text lines of indeterminate width had in a file had text "Line-to-reorder", and I only wanted to flip and display the order of the first three tokens I can do: # cat file.txt | awk '/Line-to-reorder/ { print $3 $2 $1 }' How can I let lines of text that don't have the matching criteria pass through unaltered? Secondly, How can I di...

Extracting multiple columns without loop

Hi folks, I am writing an awk script that will take the output of grep and nicely format that into an HTML table. The delimiter is the ":" character; the problem I'm running into is that that character can appear in the text as well. So if I just use $1, $2, and $3 for the filename, line number, and comment respectively, I lose anythi...

Use awk to add namespace to C# files

I have several C# classes where the namespace wasn't added to the file. A sample file looks like this (Test.cs): using System; using System.IO; public partial class MyTestClass { protected void MyTestVoid() { // Magic happens here } } As you can see there is no namespace declaration. I want the output to be: usin...

awk and printf in bash

Hi, I am trying to get the rounded number of the average load in the past 5 mins. So here goes my command: uptime | awk -F, '{print $5}'|printf "%.0f\n" It seems incorrect as it always give me 0. If I tried to use a variable as intermediate between awk and printf, then it is correct avgload=$(uptime | awk -F, '{print $5}') prin...

select row and element in awk

Hi, I learned that in awk, $2 is the 2nd column. How to specify the ith line and the element at the ith row and jth column? Thanks and regards! ...

How can I collapse this bash command line into an awk statement?

I've created an awk script and use it like this: # grep -E "[PM][IP][DO][:S]" file.txt | awk-script How can I modify the awk script to include the effort of the grep command (which is searching for either "PID:" or "MPOS"? awk-script is: #!/usr/bin/awk -f /Sleeve/ { printf("%8d, %7d, %7.2f, %7.2f, %7.2f\n", $5, $6, $7, $30,...

Count lines with awk, regardless of line ending style

What's the best way to count the number of line in a file with awk, regardless of line-ending style (DOS/Windows, UNIX, etc.)? ...

How could I print an array slice in awk?

In a file I am reformatting, I would like to put the last column as the first and have the rest of the columns stay the same. I could do this easily in python, but thought I'd learn some awk this evening. Here is an example: (before) polk_describes:1 diatribe_that:1 #label#:negative (after) #label#:negative polk_describes:1 diatri...

Format of File using Awk in PowerShell

All I did was ls to a file, then ran a simple awk print. I'm new to both PowerShell and Awk, but the output is obsviously not what's expected. Can anyone explain this? Does it have something to do with the format of the file? PS C:\Documents and Settings\lmoser\My Documents\Test> awk '{ print }' lsfiles.txt > awkedlsfile.txt PS C:\Do...

Converting FASTQ to FASTA with SED/AWK

I have a data in that always comes in block of four in the following format (called FASTQ): @SRR018006.2016 GA2:6:1:20:650 length=36 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGN +SRR018006.2016 GA2:6:1:20:650 length=36 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!+! @SRR018006.19405469 GA2:6:100:1793:611 length=36 ACCCGCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC +SRR...

Script for changing the sequence number on a file name

I am writing a shell script and I am stuck. The requirement is: I will receive files which have a sequence number on them like xyz0001abcd.DAT. I create a copy of that file, keeping the sequence number, as abcd000001gfh.DAT. The original filename uses four digits (up to 9999), and the copied one uses six (up to 999999). I am stuck when...

Shell script for getting the sequence and changing it?

I have a command like echo "abcd0001gfh.DAT" | sed 's/^[^0-9]*\(....\).*$/\1/' | awk '{ print "00"$0 }' This will give me an output of 000001. But I want to run this in a loop where I receive the file name from 0001-9999 and again it becomes 0001. So my output should like below abcd0001gfh.DAT 000001 abcd0002gfh.DAT 000002 . . . a...

Appending Text To the Existing First Line with Sed

I have a data that looks like this (FASTA format). Note that in comes with block of 2 ">" header and the sequence. >SRR018006 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGN >SRR018006 ACCCGCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC What I want to do is to append a text (e.g. "foo" in the > header) yielding: >SRR018006-foo NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN...

script,unix,compare

i have two files ... file1: 002009092312291100098420090922111 010555101070002956200453T+00001190.81+00001295.920010.87P 010555101070002956200449J+00003128.85+00003693.90+00003128 010555101070002956200176H+00000281.14+00000300.32+00000281 file2: 002009092410521000098420090709111 010560458520002547500432M+00001822.88+00001592.96+00001...

connect to a DB inside awk script

Hi In a shell script we can connect to a Database using sqlplus on unix. can i perform the same thing inside an awk script? i need to access the output of a select query inside an awk script.is that possible? ...

Compares lines in two files

how could we compare lines in two files using a shell script. I want to compare line in one file with the line in other.diff will give me all the differences in two files at a time.i want a line in the first file to be compared with all the lines in the second file and get the common lines as the output. with the line numbers where the ...

How can I embed arguments in an awk script?

This is the evolution of these two questions, here, and here. For mine own learning, I'm trying to accomplish two (more) things with the code below: Instead of invoking my script with # myscript -F "," file_to_process, how can I fold in the '-F ","' part into the script itself? How can I initialize a variable, so that I only assign a ...

awk command -to accept two variables as parameters and return a value

I have a file which has 50 rows. Each row is made up of three columns. The first two columns are the variables and this will be passed as parameters to return the 3rd column's value. for ex.. command_file.txt is the file and it contains A B 10 C D 20 E F 30 G H 50 I J 70 ... I have a script with the following command. #!/user/bin/...

problem in a shell command

Hi, i am trying the following command on the command line ps -u `id | cut -f2 -d"=" | cut -f1 -d"("` -f | grep ppLSN | awk '{print $9}' | awk '{FS="=";print $2}' | grep KLMN | wc -l the value of teh command is returned as 7. but when i am putting the same command inside a script abc_sh like below ps -u `id | cut -f2 -d"=" | cut ...