0

I am trying to print the multiple columns from the file with different sub commands with the awk command.

Below is my command which throws error while using concatenate command with awk

awk -F\| '{if(length($1) == 12 && $21 == "SOUTHWEST") print $1 "," substr($2,5,9) "," c == $3$15; print  c }' sample.txt | head > test.csv

error thrown as "syntax error near c"

sample.txt will contains data like

0011D959A6BC|308-452591505|70605|1|1|TCD2000||LK012|0|||1|0||2581|850|La‌​ke Charles, LA|308|||SOUTHWEST|null|Lake Charles, LA|1|A9200019036CF2B|1|1|0 

Expected output: 0011D959A6BC,452591505,706052581

I used below syntax as example to concatenate in my script

echo '12345|123|6789'| awk -F\| '{c=$1$2; print c}' 
  • 1
    Post the error along with an input sample (and expected output). – don_crissti Feb 28 '16 at 17:08
  • What is c here? What is c == $3$15, is it supposed to be assignment or comparison?... – Andrew Miloradovsky Feb 28 '16 at 17:11
  • sample.txt will contains data like **0011D959A6BC**|308-**452591505**|**70605**|1|1|TCD2000||LK012|0|||1|0||**2581**|850|Lake Charles, LA|308|||SOUTHWEST|null|Lake Charles, LA|1|A9200019036CF2B|1|1|0 Expected output: 0011D959A6BC,452591505,706052581 syntax error near c I used below syntax to concatenate echo '12345|123|6789'| awk -F\| '{c=$1$2; print c}' – SaravanaKumar TR Feb 28 '16 at 20:21
  • that's unreadable in a comment. by 'post the error...', @don_christi meant 'edit your question and add the additional information (and format it correctly)', not 'post a comment'. – cas Feb 28 '16 at 23:39
0

In the example you found, the c variable is useless, you can directly concatenate strings (here fields) and print the result.

Here is a way to achieve what your want to do:

awk -F\| '{
  if(length($1) == 12 && $21 == "SOUTHWEST")
    printf("%s , %s, %s\n",$1 ,substr($2,5,9),$3$15)
  }' sample.txt | head > text.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.