Defcon
Defcon

Reputation: 817

Extract Columns from file and print to new file with delimiter Shellscript

I want to print the 1,2,3,6 the columns of a delimited file with the same delimiter. The delimiter is 1e(hex) or 036 (octal). This is bashscript. I read from posts that my attemp was a good way to do this:

awk 'BEGIN{OFS="\036"} {print$1,$2,$3,$6}' filename > newfile.txt

When I do this, the file contains everything from the source file. Looking to see what I did wrong. Below I have some sample input and output. The actual file has many records. The ^^ is a representation of 1e(hex) or \036(octal).

Input:

Q001^^bob123^^001^^X^^ ^^^^ ^^               ^^TX^^B^^ ^^   ^^2^^10

Output:

Q001^^bob123^^001

Upvotes: 0

Views: 131

Answers (1)

chepner
chepner

Reputation: 532333

OFS is the output field separator, not the input field separator. You have to set FS as well.

awk 'BEGIN{FS=OFS="\036"} {print$1,$2,$3,$6}' filename > newfile.txt

Upvotes: 3

Related Questions