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

SQL Query [Count the result of a query]

0  Asked on February 3, 2021 by abdelhalim

     

function stops when fetch() fails

2  Asked on February 2, 2021 by uncannyorange

 

merging lists of dataframes in R effectively

1  Asked on February 2, 2021 by user5813583

   

The Math behind a tesorflow.tensordot()

0  Asked on February 1, 2021 by harsh-dhamecha

     

How to check if a word contains one letter from another string

3  Asked on February 1, 2021 by umar-zahid

   

How to use citeproc-java on Android

1  Asked on February 1, 2021 by hector

     

Laravel whereHas Filter sub relation

1  Asked on February 1, 2021 by bryant-tang

 

How to check if list contains dict element with same key

1  Asked on February 1, 2021 by radosaw-hryniewicki

     

react-navigation nested drawer items

1  Asked on February 1, 2021 by caner-akmak

     

Add a header to an outgoing request by a filter in WebFlux

1  Asked on February 1, 2021 by jae-young-lee

     

Ask a Question

Get help from others!

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