Fetch required column from a file with \001 delimiter

1

I am storing output of pig file in a directory Which contains lines with multiple columns separated by delimiter \\u0001.

Now in shell script I want to fetch 7th column field from the output.

I tried below commands however didn’t get required output:

cnt=`awk -F '^A' '{print $7,$1}' $f`
cnt=`cut -d'\\u0001' -f8 $f | awk '{s+=$0}END{print s+0}'`

How to achieve the same?


Here is sample file:

printf "c1\\u0001c2\\u0001c3\\u0001c4\\u0001c5\\u0001c6\\u0001c7\\u0001c8" > 001.txt

user

Posted 2015-12-17T12:18:21.690

Reputation: 199

Answers

2

Try the following syntax:

awk -F '\001' '{print $7}' < myfile

where:

  • -F - use the next arg as field separator,
  • '\001' - method of expressing a byte with a value of 1,
  • '{print $7}' - for every line of input, output the 7th field followed by a newline,
  • < myfile - use myfile as input.

Bing Bang

Posted 2015-12-17T12:18:21.690

Reputation: 159

Can you expand your answer to explain what this code does and how it addresses the problem? Unexplained code is discouraged, because it doesn't teach the solution (and RTFM isn't a substitute). Your answers will attract more upvotes with some explanation. Thanks.

– fixer1234 – 2016-03-04T19:58:14.163

This answer should have been explained. – MariusMatutiae – 2016-03-05T11:34:35.267

awk - the program – Bing Bang – 2016-03-10T17:03:52.393

1

You can use cut command for that, e.g.

cut -d $'\001' -f7 001.txt

Where -d is your delimiter and -f is your column number.

kenorb

Posted 2015-12-17T12:18:21.690

Reputation: 16 795