1

I am trying to print out columns that are separated by tabs.

First, I use find command to identify a list of files to process.

Second, I parse the file path to generate the filename to be used for output later using split.

Third, I print out a few columns that I needed (including the first column is combined from a few columns with underscore) in the tab-delimited format specified using -v OFS='\t'.

Forth, I print out the output path and the file name ended with _step3.

INPUT="/Users/dir/in"
OUTPUT="/Users/dir/out/"
find "$INPUT" -type f -iname "*.txt_cap.txt" -execdir awk -v OFS='\t' var="$OUTPUT" '{n = split(FILENAME,p,"/"); print ($1"_"$2"_"$4"_"$3, $2, $5, $6, $7) >> (var p[n] "_step3")}' {} +

I got the following error message. What went wrong?

awk: syntax error at source line 1
 context is
     >>>  <<< 
awk: bailing out at source line 1

UPDATE - based on the suggestion from @glennjackman after adding -v for each variable, there is a different error message.

INPUT="/Users/dir/in"
OUTPUT="/Users/dir/out/"
find "$INPUT" -type f -iname "*.txt_cap.txt" -execdir awk -v OFS='\t' -v var="$OUTPUT" '{n = split(FILENAME,p,"/"); print ($1"_"$2"_"$4"_"$3, $2, $5, $6, $7) >> (var p[n] "_step3")}' {} +

I got the following error message:

awk: can't open file MAP_pv1.txt_cap.txt
 source line number 1

A side question, when I use find "$INPUT" -type f -iname "*.txt_cap.txt" I could see a list of files shown on the terminal. I was also able to see the files displayed when using "$INPUT" -type f -iname "*.txt_cap.txt". But when combined with awk, the first one with longer file matching pattern, I was able to get error messages for the command, but there was no error messages and no output files generated when just .txt used as matching pattern. Does anyone know the reason for the silence behavior?

Thank you.

  • 1
    What is the main question? You only seem to have the side question there. And, about the side question, what are the actual commands you are running? What is it that shows results and what does not? Awk's results will depend on the contents of your files. Can you give us an example we can reproduce? Finally, your quoting in the awk command is strange. Is that really what you use? "$2, " will print $2, and not the second field. Please edit your question, clarify what you want to ask and show us an example we can test. – terdon Mar 25 at 20:22
  • Thanks. @terdon, after drafted the question, I forgot to put down the error message I got. Very embarrassed! – Molly_K Mar 25 at 20:33
  • Yes, wrong quoting in print ($1"_"$2"_"$4"_"$3, "$2, "$5, "$6, "$7) – glenn jackman Mar 25 at 20:38
  • @glennjackman Thanks for spotting the mistake. I fixed it and still see the same error message. I don't know if there's something wrong with specifying-v OFS and -v var using the same -v. – Molly_K Mar 25 at 20:45
  • I missed that. You absolutely need to use -v for each variable – glenn jackman Mar 25 at 20:59

Your Answer

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

Browse other questions tagged or ask your own question.