0

I have a txt file (input.txt) that looks like this:

A_Karitiana-4.DG        Ignore_Karitiana(discovery).DG
A_French-4.DG   Ignore_French(discovery).DG
A_Dinka-4.DG    Dinka.DG
A_Dai-5.DG      Dai.DG
S_Dai-2.DG      Dai.DG
B_Dai-4.DG      Dai.DG
S_Dai-3.DG      Dai.DG
S_Dai-1.DG      Dai.DG

I need to create a new txt file (output.txt) that contains only the first column of input.txt. So output.txt must look like this:

A_Karitiana-4.DG        
A_French-4.DG   
A_Dinka-4.DG    
A_Dai-5.DG      
S_Dai-2.DG      
B_Dai-4.DG      
S_Dai-3.DG      
S_Dai-1.DG      

I've tried with this command:

awk '$1' input.txt > output.txt

and also with this:

awk -F' ' '$1' input.txt > output.txt

but both of them create an output.txt file that looks exactly the same as input.txt.

I suppose it's a matter of delimiter, but I can't figure out how to fix this.

3 Answers 3

5

You're not printing. Try

awk '{print $1}' input.txt > output.txt

When you just give an expression (the way you tried), awk works somewhat like default grep: completely print any matching lines:

  1. awk '/regexp/' file.txt - print lines matching regexp
  2. awk 'NR==3' file.txt - print line 3
  3. awk '1' file.txt - print all lines where 1 is true, i.e. all (okay, an awk-ward way to cat, but we're approaching what you did)
  4. awk '$1' file.txt - print all lines where $1 evaluates to true, i.e. is non-empty (and does not otherwise evaluate to false, such as "0"), i.e. given your file, print all all lines (since $1 here will always contain a non-numerical, non-empty string)
0
5

Awk scripts consist of pattern {action} pairs.

  • if pattern is empty, then {action} will be applied to all records
  • if {action} is empty, then the default action {print} will be applied to all records matching pattern

awk '$1' will apply the default action {print} when pattern $1 evaluates true. Since a non-empty string is true, it will print the whole of any non-empty records, except those whose first field evaluates numerically to zero in your locale.

Instead, you want to apply a non-default action {print $1} to the default empty pattern:

awk '{print $1}' input.txt > output.txt
2

While this is an awk question, I'd nevertheless throw cut in here as good alternative for this specific task:

cut -d' ' -f1 input.txt > output.txt

The default delimiter (tab) has been replaced with space by -d' ' and the first field selected by -f1.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.