Questions tagged [awk]

awk is a special-purpose, interpreted programming language for extracting and reporting data.

The awk utility interprets a special-purpose programming language that makes it easy to handle simple data-reformatting jobs.

Source: The GNU Awk User's Guide

The name comes from the initials of its original developers Aho, Weinberger, Kernighan who wrote this utility back in 1977.

516 questions
110
votes
5 answers

How to separate fields with space or tab in awk

While playing with awk I came to execute: ls -la >> a.txt ; awk {'print $5 $1'} a.txt ; This is giving output like: 53277-rw------- 52347-rw------- How can I get a space between these two friends of output?
Raja G
  • 105,327
  • 107
  • 262
  • 331
45
votes
2 answers

What does this command mean: awk -F: '{print $4}'?

Please explain what does following command mean: awk -F: '{print $4}'
PHP Learner
  • 2,948
24
votes
5 answers

How can I merge files on a line by line basis?

cat file1 foo ice two cat file2 bar cream hundred Desired output: foobar icecream twohundred file1 and file2 will always have the same amount of lines in my scenario, in case that makes things easier.
TuxForLife
  • 1,333
17
votes
5 answers

How to delete lines starting with certain strings

Is there a way to delete lines starting with certain strings. I have this youtube-dl code youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E and its result is like…
17
votes
5 answers

How can I process multi-line records with awk in a bash script?

example.txt is below Restaurant: McDonalds City: Miami State: Florida Address: 123 Biscayne Blvd Phone: 911 Restaurant: 5 guys City: Atlanta State: Georgia Address: 123 Peachtree Rd Phone: 911 Restaurant: KFC City: NYC State: NY Address: 123…
15
votes
5 answers

Difference of two big files

I have "test1.csv" and it contains 200,400,600,800 100,300,500,700 50,25,125,310 and test2.csv and it contains 100,4,2,1,7 200,400,600,800 21,22,23,24,25 50,25,125,310 50,25,700,5 now diff test2.csv test1.csv > result.csv is different than diff…
Lynob
  • 6,775
13
votes
3 answers

Extract word from string using grep/sed/awk

I have a string 00:28:04 /usr/lib/jvm/jre/bin/java -DJDBC_CONNECTION_STRING= -Dspring.profiles.active=qa -XX:MaxPermSize=256 and want to extract the word qa that follows -Dspring.profiles.active. I have the string save in a file text.txt just to…
Gman
  • 491
13
votes
3 answers

How do I replace multiple lines with single word in file(inplace replace)?

Content of my filename file is as following(for example): My block of line starts from here START First line second line third line END and end to here for example. I want to replace block of lines between START and END with just a single word,…
αғsнιη
  • 36,350
12
votes
1 answer

Do I need to install awk or is it inbuilt in Ubuntu?

I'm new to Linux OS and studying Mobile Computing. I have to analyze files using awk. I'm using Ubuntu 14.04. Do I need to install awk or is it inbuilt in Ubuntu?
NIMISHAN
  • 1,625
  • 4
  • 20
  • 28
12
votes
6 answers

grep to return Nth and Mth lines before and after the match

I know that with grep I can use the fields -A and -B to pull previous and next lines from a match. However they pull in all lines between the match based on however many lines are specified. grep -r -i -B 5 -A 5 "match" I'd like to only receive…
chollida
  • 225
11
votes
3 answers

awk - How to print the number of characters for the first n lines in a file?

I have a command: $ awk '{ print length($0); }' /etc/passwd It prints number of characters of every line in a passwd file: 52 52 61 48 81 58 etc. How can I print the number of characters for only the first n lines? For example - for the first 3…
10
votes
6 answers

How to remove the first colon ':' from a timestamp?

I am new to programming!! Can anyone help to remove the : at the first position in a timestamp: :29.06.2019 23:03:17 Presently I am trying to do it using awk/cut commands as shown below: TDS="$(grep 'Logfile started' process.log | awk '{print…
9
votes
7 answers

How to use sort on an awk print command?

I have a couple of commands in an awk script I'm writing: print "Here are some players and their numbers, sorted by last name" if(sum[x] > 500) {print x, $2} Which outputs: Here are some players and their numbers, sorted by last name Lebron James…
Anonymous
  • 425
9
votes
5 answers

Only get the H+W geometry of my screen without the later +x+y

I am reading this article. There is a statement there that goes: ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -r 25 -i :0.0 -sameq -f mpeg -ar 48000 -s wvga -y sample.mp4 When I run the…
9
votes
4 answers

How would you separate fields with multiple spaces and store them in an array?

In my file mytxt: field1 field2 ------ ------- this are numbers 12345 this letters abc def ghi Let's say I want to store the first field in an array: i=0 while read line; do …
zou hai
  • 101
1
2 3
34 35