Questions tagged [awk]

AWK is a text-processing language. It is mainly used to manipulate and process structured data, and to generate reports.

AWK is a programming language created by Alfred V. Aho, Peter J. Weinberger and Brian W. Kernighan for processing text-based data, whether from files or data streams.

AWK is an example of a programming language that extensively uses the string datatype, associative arrays (that is, arrays indexed by key words) and regular expressions. The power, terseness, and limitations of programs made ​​in AWK and sed scripts inspired Larry Wall to create the Perl language.

642 questions
209
votes
18 answers

Removing ANSI color codes from text stream

Examining the output from perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";' in a text editor (e.g., vi) shows the following: ^[[37mABC ^[[0m How would one remove the ANSI color codes from the output file? I…
user001
  • 3,994
65
votes
4 answers

awk, mawk, nawk, gawk... WHAT?

I've just started learning awk and I'm a little confused about all those versions around. Is there any "version" which is found on all Unix-like systems? Like, you know, plain vi? Does the standard awk support the -F option?
Trollhorn
52
votes
11 answers

sed: how to replace line if found or append to end of file if not found?

With a single input file that only contains comments (starting with #) and VARIABLE=value lines, is it possible to replace a value for a single variable if found and, otherwise, append the pair to the end of file if not found? My current method…
BlakBat
  • 1,279
23
votes
6 answers

How to get the pid of a running process using a single command that parse the output of ps?

I am looking for a single line that does return the pid of a running process. Currently I have: ps -A -o pid,cmd|grep xxx|head -n 1 And this returns the fist pid, command. I need only the first number from the output and ignore the rest. I suppose…
sorin
  • 12,189
19
votes
7 answers

How can I replace a newline with its escape sequence?

Using sed to make text that contains certain characters suitable for use in string literals is straightforward: sed "s/\\\\/\\\\\\\\/g" sed "s/\\\"/\\\\\\\"/g" But how can I do something similar with a text file containing newline characters?
Melab
  • 1,219
19
votes
2 answers

How can I find my awk version?

If I want to know the version of awk I get the following: $ awk --version awk: not an option: --version Checking in man awk I see that my awk is mawk - pattern scanning and text processing language
fedorqui
  • 2,027
19
votes
2 answers

replace nth occurence of string in each line of a text file

I have large text files with space delimited strings (2-5). The strings can contain "'" or "-". I'd like to replace say the second space with a pipe. What's the best way to go? Using sed I was thinking of this: sed -r 's/(^[a-z'-]+ [a-z'-]+\b)…
dnkb
  • 387
15
votes
7 answers

Piping tail -f into awk

I am trying to pipe tail -f into awk to monitor a logfile in realtime, but according to examples, there should be not problem but I can't get it to work. here is the command I'm running tail -f logfile.log | awk -F" " '{print $1, $2, $7, $8}' But…
gimpycpu
  • 251
15
votes
4 answers

How to pipe awk output (with periodic, continuous input) to output file?

I am trying to write a command that pipes the continuous output of a free command (run every second) to an awk command that parses a specific value (available free memory) and outputs this to a file with a timestamp. Here are my current attempts at…
Mark
  • 380
15
votes
3 answers

How can I make a table of contents for a markdown document with Python/AWK/SED?

I have the following markdown document: Heading-a ========== ---text--- Heading-b ------------ --- text --- Heading-c ---------- --- text--- Heading-d ======= --- text---- Heading-e --- ... I would like to make a clickable table of…
14
votes
6 answers

Randomly shuffle rows in a large text file

I have a text file of ~1GB with about 6k rows (each row is very long) and I need to randomly shuffle its rows. Is it possible? Possibly with awk?
ddmichael
  • 387
14
votes
2 answers

How can I identify non-ASCII characters from the shell?

Is there a simple way to print all non-ASCII characters and the line numbers on which they occur in a file using a command line utility such as grep, awk, perl, etc? I want to change the encoding of a text file from UTF-8 to ASCII, but before doing…
user001
  • 3,994
14
votes
5 answers

Add to every end of line in Notepad++

I have a long text file gallery-dl -g -i w4b027.txt > gallery-dl -g -i a4b028.txt > gallery-dl -g -i b4b029.txt > gallery-dl -g -i c4b030.txt > gallery-dl -g -i d4b031.txt > gallery-dl -g -i w4b032.txt > gallery-dl -g -i w4b033.txt > gallery-dl -g…
Roxion
  • 354
13
votes
3 answers

How can I cat/print a file except the last 2 lines?

How can get I get the contents of a text file, except the last (for instance) 2 lines, with standard or GNU shell tools? (bash, tail, cat and so on.)
13
votes
1 answer

How to use awk separator of single quote?

I'd like to use awk to pull out information from SQL output like the following: (count(distinct serverclass)='2') And need to extract the number, which is 2 in this example. I'd like to do something like the following, but cannot figure out how to…
WilliamKF
  • 8,058
1
2 3
42 43