awk - pattern-directed scanning and processing language

learn more… | top users | synonyms

1
vote
6answers
5k views

How do use awk along with a command to show the process ID with the 'ps' command

I'm able to to show the PID (2nd) column with the below command, but instead I want to show pwdx <PID> and also the COMMAND(8th) column. Thanks in advance, Chirag ps -ef | grep runner | awk ...
3
votes
1answer
380 views

Relative line offsets in awk (/pattern/+1 or /pattern/-1)

When I want to print lines between two patterns, excluding the lines with those patterns, I can do it in ex using +1 and -1 after the pattern. That is awesome. Is this possible in awk? Right now, I ...
4
votes
3answers
2k views

Show lines matching a pattern and the 4 lines before each

For example, from this file: CREATE SYNONYM I801XS07 FOR I8010.I801XT07 * ERROR at line 1: ORA-00955: name is already used by an existing object CREATE SYNONYM I801XS07 FOR ...
6
votes
2answers
3k views

How to use regex as field separator in awk?

I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right. rpm -qa | awk '{ 'FS == [0-9]' ; print $1 }' awk: cmd. line:1: { FS awk: ...
1
vote
1answer
277 views

What does this awk script do

echo 1 | awk '{ srand(); print int (rand()*67) }' I get a different o/p everytime I run it. However if I omit the 1 I get same o/p ..why?
5
votes
1answer
130 views

Inderect references aka value of a value in awk

I'm searching for a way to solve this problem in awk. Input: X 1 Y 2 Z 3 X 4 Y 5 The output should look like this: X 5 Y 7 Z 3 I see two problems here: the first one is the indirect reference in ...
3
votes
3answers
196 views

What does . match?

In working with regular expressions, I have been told that a dot character . will match everything. Except for newlines \n. Are there any other exceptions? What about the NUL character \0, or the ...
1
vote
1answer
545 views

How to manipulate awk variable NF in zsh?

I have a simple command line: uptime | awk '{print $(NF - 2)}' In a bash shell, this works without a hitch, but when I run it in zsh, I get the following error: title:5: command not found: NF As ...
1
vote
3answers
1k views

How to awk the “df -Pm” command to get FS names that have more then 90% the FS usage?

I'm trying to get the names of all disks with at least 90% usage, using this oneliner: df -Pm | awk '+$5 >= 90 {print}' But it doesn't show the "100% usage" case, I need to grep for "100%". Why ...
3
votes
1answer
175 views

Help improving my AWK skills

This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations) Could some one improve ...
6
votes
1answer
609 views

Why are capital letters included in a range of lower-case letters in an awk regex?

$ echo ABC | awk '$0 ~ /^[a-b]/' ABC $ echo ABC | awk '$0 ~ /^[a-a]/' $ echo ABC | awk '$0 ~ /^a/' $ You see. /[a-b]/ captures A, but /[a-a]/ or /a/ doesn't. Why?
4
votes
2answers
779 views

How to test if a function is defined in awk?

I am writing an awk program and I want it to run it both with mawk and gawk. I need a sorting function. I have written my own, but I want to use the builtin asort() function in gawk if available. How ...
4
votes
4answers
2k views

How to echo an escaped string

How can I echo an escaped string that contains $ in Bourne Shell? user@server:~$ cat test.sh #!/bin/sh echo $1 user@server:~$ ./test.sh \$sad\$test $sad$test I want it to return an escaped ...
2
votes
1answer
266 views

Awk : Range of PIDs

I am learning awk from this tutorial. It's quite basic. I have a list of processes in a file which I got by doing ps aux > processes Now according to the tutorial, doing awk '$2 ~ 14022, $2 ~ ...
3
votes
3answers
1k views

In awk, how to print a range of fields with the original $0 delimiters intact?

How to extract a substring of $0, in awk, when that substring's position and length is based on the positions of fields in the original $0? It effectively equates to removing the first n.lead fields ...
3
votes
2answers
2k views

How would I extract the user agent strings from a log file?

Currently I am running a command like this, to get the most requested content: grep "17\/Jul\/2011" other_vhosts_access.log | awk '{print $8}' | sort | uniq -c | sort -nr I want to now see the user ...
2
votes
2answers
419 views

maximum of column with matching id

I have a file with the following fields NM001 rp1 210 NM003 rp1 220 NM005 rag 200 NM004 rag 100 I would like to have an output that for every change in column2, find maximum in column4 but also ...
1
vote
1answer
85 views

awk expand window to 500% for two numbers from column file

I've got a file with two columns corresponding to start-end ranges like this: awk '{print $2"-"$3}' file.txt 91082802-91082990 94018177-94018321 146945732-146945949 88701397-88701594 ...
4
votes
1answer
2k views

awk: forcing a return status?

This is a followup to my earlier question. I am validating the number of fields in /etc/passwd using this handy snippit. In the following example, the users 'fieldcount1' and 'fieldcount2' have the ...
1
vote
1answer
667 views

Inconsistent field separator behaviour of space in awk

The following awk script shows how a leading space ' ' is ignored as a field separator, but other characters are treated as a separator with a preceding null $1 field. Is there some way to make ...
-1
votes
1answer
2k views

Sum of grep results

I need to output the sum of a grep results for a certain time, for now i hard coded the time grep but any help will be very much appreciated, my problem is i cant output the results on the script. If ...
2
votes
1answer
2k views

Time/Date grep for 15 mins

I need to grep a specific type of line in a specific time of the log, do you have any ideas? Here's my working script: cat *.log |grep -E '2011-06-30 (1[0-1]:[0-1][0-5]|10:16)'| grep -ach '0110 ...
1
vote
1answer
296 views

Need help processing a text file with awk to conform to CSV flat file format

I have the following problem. I collected data on reaction time from over 100 participants for an experiment I am running. Unfortunately, the separators between fields were not consistent, but after a ...
3
votes
2answers
2k views

only display df lines that has more fs usage then 80%

root@SERVER ~$ df Filesystem 512-blocks Free %Used Iused %Iused Mounted on /dev/YXCV 655360 365632 45% 6322 13% / /dev/ASDF 3801088 670648 83% 41759 32% /usr /dev/ASR ...
2
votes
3answers
659 views

pad a number with zeroes

I need have put 0 on the results of my grep so my script format will be fine and i don't have any idea how to do it. here's my grep result : 261 : 261 = 0 | 1192 : 1184 = 8 | 283 : 283 = 0 | 666 : ...
10
votes
3answers
6k views

Delete the first n bytes of files

I've got an extreme problem, and all of the solutions I can imagine are complicated. According to my UNIX/Linux experience there must be an easy way. I want to delete the first 31 bytes of each file ...
3
votes
1answer
7k views

Filtering on dates with grep and awk

I have created the alias below in my .bash_aliases file alias auth="grep \"$(date|awk '{print $2,$3}')\" /var/log/auth.log | grep -E '(BREAK-IN|Invalid user|Failed|refused|su|Illegal)'" ...
5
votes
4answers
1k views

How to append end of every line with the line itself

This is in file.txt: redcar bluecar greencar Im looking for ways to make it become: redcar redcar bluecar bluecar greencar greencar I've tried many ways using sed with no luck
3
votes
1answer
1k views

using system command in awk script

In an AWK script I am using a command system(date) to print the current date in a file but after this command is executed next line is also added implicitly. Is there any way in AWK to print ...
6
votes
2answers
3k views

sed one-liner to delete any line that does not contain lowercase letters

So, basically THIS LINE WOULD BE DELETED and (THIS LINE WOULD ALSO BE DELETED) but Indeed, THIS LINE WOULD NOT
4
votes
2answers
3k views

My awk program to change spaces into tabs doesn't work

How to write a shell script which uses awk to read in the data file students.txt and output the data in the tabbed format as shown: Surname Forename MSc Stream Date of Birth Smith John IT 15.01.1986 ...
3
votes
4answers
261 views

Remove empty configuration section

Files like ~/.config/vlc/vlcrc are 99% junk if you want to version control only the configuration options. I've got a script to remove the comments, but there's a ton of empty configuration sections ...
2
votes
3answers
6k views

Loop a list through awk

I'm sorry if this is extremely elementary, but I just can't figure out how to do this, and my research has failed me as well. I have two files: data.csv and list.txt. Here's an example of what they ...
3
votes
2answers
365 views

Truncate a file on a certain pattern

How would I go about truncating a binary file when a certain pattern is found? For instance, I want to truncate the file at the first occurrence of the pattern 0xFFFFFFFF. I think something like awk ...
2
votes
3answers
3k views

Extracting tokens from a line of text

Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
0
votes
2answers
1k views

How to run a shell script containing an awk command

How to run this script (called count.sh)? awk 'BEGIN{ x=0 ; while (x < 5) { x+=1 ; print x ; } }' I'm trying to run it with sh count.sh but it's giving me an error.
2
votes
2answers
1k views

How to build a long command string?

I've a sequence of commands to be used along with lot of pipings, something like this: awk '{ if ($8 ~ "isad") {print $2, $5, "SE"} else {print $2, $5, "ANT"} }' ...
5
votes
3answers
842 views

extracting “tag” information from git with a shell script

The vc bundle is a neat little package that extracts information about a git repo for easy insertion into a LaTeX document. It doesn't currently extract information about whether the current commit is ...
5
votes
4answers
227 views

How could I simplify this command to only use awk?

awk '/user/ {print $1 }' /etc/userdomains | sed 's/://' the format of /etc/userdomains is domain.tld: user otherdomain.tld: otheruser