Visit Our UNIX and Linux User Community

Why awk print is strange when I set FS = " " instead of FS = "\t"?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why awk print is strange when I set FS = " " instead of FS = "\t"?
# 1  
Old 07-24-2018
Why awk print is strange when I set FS = " " instead of FS = "\t"?

Look at the following data file( which has four fields separated by tab.

Four fields are country name, land area, population, continent where it belongs.

As for country name or continent name which has two words, two words are separated by space.

(Data are not accurately confirmed, just for test purpose)

USSR    8649    275    Asia
    Cananda    3852    25    North America
    China    3705    1032    Asia
    USA    3615    237    North America
    Brazil    3286    134    South America
    India    1267    746    Asia
    Mexico    762    78    North America
    France    211    55    Europe
    Japan    144    120    Asia
    Germany    96    61    Europe
    England    94    56    Europe
    Taiwan    55    144    Asia
    North Korea    44    2134    Asia


awk 'BEGIN { FS = "\t" } { print $1, "---", $4 }'

I got the output which exactly meets my anticipation:

USSR --- Asia
    Cananda --- North America
    China --- Asia
    USA --- North America
    Brazil --- South America
    India --- Asia
    Mexico --- North America
    France --- Europe
    Japan --- Asia
    Germany --- Europe
    England --- Europe
    Taiwan --- Asia
    North Korea --- Asia

Then I replace \t by one space (" ")
That is :

awk 'BEGIN { FS = " " } { print $1, "---", $4 }'

The output I got is not understandable to me

USSR --- Asia
    Cananda --- North
    China --- Asia
    USA --- North
    Brazil --- South
    India --- Asia
    Mexico --- North
    France --- Europe
    Japan --- Asia
    Germany --- Europe
    England --- Europe
    Taiwan --- Asia
    North --- 2134

Line 2,4,5,7,13 each have one space and the other lines have no space(s) at all.
As for lines that have no space, why $1, $4 still can be printed ?

As for line 2,4,5,7,13, I thought $1 should be printed like this:

 Cananda    3852    25    North 
        USA    3615    237    North 
        Brazil    3286    134    South 

        Mexico    762    78    North 

And $4 does not exist.

Where did I get wrong ?

Last edited by RudiC; 07-24-2018 at 04:15 AM.. Reason: Changed QUOTE tags to CODE.
# 2  
Old 07-24-2018
Hello chihuyu,

Welcome to forums. So problem here is string/country names on 1st field which are having spaces in their names for example North Korea. So when you are setting FS as \t this string will be considered as a single field on the other hand when you will set FS as space this will be considered as 2 different fields. That is why you are seeing difference between field numbers after changing the FS values in your codes.

I would suggest your first attempt is good enough to get your expected values.
Kindly do let me know in case of any queries on same.

R. Singh

Previous Thread | Next Thread
Test Your Knowledge in Computers #894
Difficulty: Medium
A tr command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.
True or False?

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Solaris

How to check "faulty" or "stalled" print queues - SAP systems?

Hi all, First off, sorry for a long post but I think I have no other option if I need to explain properly what I need help for. I need some advise on how best to check for "faulty" or "stalled/jammed' print queues. At the moment, I have three (3) application servers which also acts as print... (0 Replies)
Discussion started by: newbie_01
0 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep ""|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep ""|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

Problem with "set" and "awk"

Hi, i'm programming on /bin/csh and i need to get the number extracted by this: set ppl_kn = $(awk '{ field = $6 } ; END{ print field }' < ppl_LM_kn.ppl ) and the output is: "Illegal variable name." Please anyone can help me what's wrong? Thanks in advance (2 Replies)
Discussion started by: tmxps
2 Replies

Featured Tech Videos