I need to concatenate two commands on shell, I'd have the path result and concatenate with the number of column in the first field.


#this gives me the path of my path directory like this: `/apps/ent/appli_ent/gen/dev/recep/ENTSMETA.20150824.txt`
find $REP_RECEP -name "*META*" -print 

I'd take the result of this command and concatenate with this:

#this gives me the number of my colmuns field.
awk -F'|' '{print NF; exit}' 

When I do this:

awk -F'|' '{print NF; exit}' find $REP_RECEP -name "*META*" -print

it doesn't work.

  • awk -F'|' 'FNR==1 {print NF}' $(find $REP_RECEP -name "*META*" -print) – JJoao Jan 15 '16 at 10:51
  • @JJoao Thanks a lot , thats exactly what i want – amine tabenyoussef Jan 15 '16 at 10:55
  • @aminetabenyoussef note that JJoao's solution breaks if your file names contain spaces or any other strange characters. – terdon Jan 15 '16 at 10:59
  • @terdon, exactly , I had a file with spaces, and your method solved the problem . thank you very much – amine tabenyoussef Jan 15 '16 at 11:04

The safest way, which can work with arbitrary file names, is to use find's -exec option. This will run the specified command on every file/directory found by find (from man find):

       -exec command ;
          Execute  command;  true  if 0 status is returned.  All following
          arguments to find are taken to be arguments to the command until
          an  argument  consisting of `;' is encountered.  The string `{}'
          is replaced by the current file name being processed [...]

So, you can do:

find "$REP_RECEP" -name "*META*" -exec awk -F'|' '{print NF; exit}' {} \;
  • Or one requiring fewer invocations of awk and therefore faster find "$REP_RECEP" -name "*META*" -exec awk -F'|' '{print NF; nextfile}' {} + – iruvar Jan 15 '16 at 15:27

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.