1

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
  • 2
    Please add your desired output (no description) for that sample input to your question (no comment). Commented Oct 26, 2020 at 12:10
  • @Bodo I have amended - sorry for wasting your time Commented Oct 26, 2020 at 15:41
  • @Maskiin Thank you. No problem. As you are new here, it's normal that you don't know all the "rules" of this site. Commented Oct 26, 2020 at 15:45

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.I have tried but still it shows like below ``` header2|header3|header4|header6|header7 column2FScolumn3FScolumn4FScolumn6FScolumn7 ```
@Maskiin Please also edit your question as suggested in the comments below your question. This site is not only to answer the question for you personally, but also serves as a reference for others that may have a similar question. That's why your question should be clear and easy to understand. Currently the answers are partially based on guessing about your requirements.
happy to edit my question - it doesn't let me. I am new to the site apologies in advance for my ignorance
@Maskiin Below your question there should be written in gray something like "share edit delete flag". Click on "edit" to edit your question.
1

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,

2 Comments

Do you mind elaborating more - perhaps using my example? Thanks
I have used your example? I have also assumed the output you need
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.