awk delimiter

# Basic syntax:
awk -F'[:/|]' '{print $field#, $field#, ...}' input_file
# Where:
#	- : / and | are the three field delimiters considered
#	- $field# is the field nuber to be printed based on the splits made
#		by any of the delimiters

# Example usage:
# Say you have a file that contains the following lines and you want to
# extract the dir#, the file #, and the URL:
/logs/dir1/file_1 = demo.example.com
/logs/dir2/file_7 = quest.example.com
/logs/dir3/file_5 = www.example.com

# Running:
awk -F'[/_=]' '{print $3, $5, $6}' input_file

# Returns:
dir1	1	demo.example.com
dir2	7	quest.example.com
dir3	5	www.example.com

# *Note, each character contained in the braces [ ] is treated as a 
#	separate delimiter

3.8
8

                                    Just use function split in awk command to split a line into an array 'a'
using a choosen string as delimiter as for example ", " in next use case:
echo "hi, bye, hey" | awk '{split($0,a,", "); print a[3],a[2],a[1]}'

3.8 (10 Votes)
0
4.4
5

                                    # by default, awk uses empty space(s) as field seperator
# we can specify field seperator ourselves using -F flag

# SINGLE field seperator
echo 'a+b=c' | awk -F'=' '{print NF, $1, $2}'
# NF in awk stands for number of fields
# Returns
2	a+b 	c

# MULTIPLE field seperators
echo 'a+b=c' | awk -F'[+=]' '{print NF, $1, $2, $3}'
# Returns
3	a	b	c


4.4 (5 Votes)
0
4
5

                                    # by default, awk uses empty space(s) as field seperator
# we can specify field seperator ourselves using -F flag

# SINGLE field seperator
echo 'a+b=c' | awk -F'=' '{print NF, $1, $2}'
# NF in awk stands for number of fields
# Returns
2	a+b 	c

# MULTIPLE field seperators
echo 'a+b=c' | awk -F'[+=]' '{print NF, $1, $2, $3}'
# Returns
3	a	b	c


4 (5 Votes)
0
4.6
5
Mad_bong 95 points

                                    Just use function split in awk command to split a line into an array 'a'
using a choosen string as delimiter as for example ", " in next use case:
echo "hi, bye, hey" | awk '{split($0,a,", "); print a[3],a[2],a[1]}'

4.6 (5 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source