AnswerBun.com

How to print a column using awk command line and add header

Stack Overflow Asked by Maskiin on November 4, 2020

I have a problem and would appreciate any assistance. I have test1.txt file with the following columns:

column1|column2|column3|column4|column5|column6|column7|column8|

I then run the following to target specific columns.

cat test1.txt | awk -F "|" '{ print $2, $3, $4, $6, $7}' > teste.csv 

The CSV file outputs the result in 1 line like so:

column2 column3 column4 column6 column7

but what i want is something like

header2|header3|header4|header6|header7
column2|column3|column4|column6|column7

Would you be able to help me fix the above and – also how can i then add headers after getting the correct result.

Thanks in advance

3 Answers

If the problem is that the columns should be separated with | instead of space, you have to set the output field separator OFS to the same value as the input field separator FS.

awk -F "|" -v 'OFS=|' '{ print $2, $3, $4, $6, $7}' test1.txt > teste.csv

You can use the BEGIN condition to add commands for printing a header. In this case you also can put the assignement to OFS there.

awk -F "|" 'BEGIN { OFS=FS; print "header2|header3|header4|header6|header7" } { print $2, $3, $4, $6, $7}' test1.txt > teste.csv

With the input shown in the question this prints

header2|header3|header4|header6|header7
column2|column3|column4|column6|column7

Correct answer by Bodo on November 4, 2020

Given that you have referenced a csv (comma separated value output in your question), wit th sample data in the file columns, you can run:

awk -F| 'BEGIN { OFS=",";print "COLUMN1"OFS"COLUMN2"OFS"COLUMN3"OFS"COLUMN4"OFS"COLUMN5"OFS"COLUMN6"OFS"COLUMN7"OFS"COLUMN8"OFS } { $1=$1 }1' columns

Set the output to , in the BEGIN section and print the headers required (in the example COLUMN1,COLUMN2 etc) with the new OFS. Then take each line, set $1 to $1 to action a change on $0, taking account of the new OFS. Use 1 to print the new line.

Output:

COLUMN1,COLUMN2,COLUMN3,COLUMN4,COLUMN5,COLUMN6,COLUMN7,COLUMN8,
column1,column2,column3,column4,column5,column6,column7,column8,

Answered by Raman Sailopal on November 4, 2020

To add the headers

awk 'BEGIN {print "Column1|Column2|Column3|Column4|Column5|Column6|Column7|Column8"} {print}' table > test1.txt

To add the columns you would like, into the teste.csv

cat test1.txt| awk -F '|' '{print $2, $3, $4, $6, $7}' > teste.csv

Answered by Shaqil Ismail on November 4, 2020

Add your own answers!

Related Questions

Removing duplicates while sorting numbers inside a String in java

5  Asked on November 30, 2020 by swetha-haridoss

       

pointers cant read the correct elements in array

2  Asked on November 29, 2020 by jabou

   

pandas does not load the sub packages properly

0  Asked on November 29, 2020 by realbro

   

Webpack: how to copy html files into build folder?

2  Asked on November 29, 2020 by wai-yan-hein

   

Save string to file without converting newlines using Python

1  Asked on November 29, 2020 by kleiton-kurti

       

ngx-mask Do not allow negative value for the currency input

2  Asked on November 28, 2020 by ashot-aleqsanyan

   

Unity: Add extra time to slider

1  Asked on November 28, 2020 by christopher-madsen

     

Python cannot access list

3  Asked on November 28, 2020 by forge-mods

   

Get property name and value only if there is a value

1  Asked on November 28, 2020 by jimenemex

   

Count percentage of upper case words

1  Asked on November 28, 2020 by user13623188

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP