Help on grep syntax in UNIX

Dear Team

/app/Appln/logs/
 echo Session used server are  'grep -i pid|grep -i session | cut -d'.' -f1  | awk '{print $9}' | sort | uniq'

Output -

lxserver01
lxserver02
 lxserver03

When I grep session pid in logs server details I can see above distinct server details but I want to print output like
Session used server are :- lxserver01,lxserver02,lxserver03

Please advise

How can your output be

lxserver01
lxserver02
lxserver03

When you've clearly ran

echo Session used server are 'grep -i pid|grep  .. blah blah...

Please state your problem legibly, using code tags, please.

Hi Scott,

Please find my code...Pls help.

#!/bin/bash

if [$1 = ""];then
   echo "Please enter Env name to see the allocation Details"
   exit 1
fi

cd /app/APPLN/$1/logs

echo "$1 Env Allocation Details are mentioned below"

BL_LAYER=`ls -ltr *log |grep -i log|grep -i session |  cut -d'.' -f1  | awk '{print $9}' | sort | uniq| paste -s`
echo "Business Layer are :-  $BL_LAYER"

Output - Business Layer are :- lxserver01 lxserver02 lxserver03

Expected output to print- Business Layer are :-lxserver01, lxserver02,lxserver03

With a looong pipe like that, you could certainly add a tr '\n' ',' to achieve what you need. On the other hand, as you deploy awk anyhow, why not do ALL of it in one awk script?
AND, why use the -ltr options to ls when you need the file name only?

The two grep s for "log" and "session" are somewhat redundant, as "log" HAS to be in the file names due to the pattern for the ls command, and "session" as well could be added to the pattern.

Since many versions of awk do not include a sorting function and you seem to want sorted output (although it isn't clear if you want unique sorted output or just need sort to get unique output), you might try a simpler pipeline:

BL_LAYER=`ls *session*log | cut -d'.' -f1  | sort -u | paste -s -d, -`
echo "Business Layer are :- $BL_LAYER"

If the cut in this pipeline is just getting rid of .log at the end of your filenames, the sort -u can be removed from the pipeline and you'll still get the same results.

Note that the above pipeline uses a comma as the separator between filenames instead of the <tab> used as the default by paste . If you want the delimiter to be <comma><space> sometimes and just <comma> sometimes (as in your latest ";i'expected output to print[/i]"), you'll need to find another way to do that. (The paste utility only uses single characters as field delimiters, and you'll need to very clearly define the conditions under which each of the various delimiters you want to use are supposed to be chosen as the delimiter between output filenames!)

Do you need all the detail provided by the ls -l ? It might be easier to write a bit like this:-

for file in *log              # Loop for each file matching.  You may want to use *session*log instead
do
   printf "${file%%.*}\n"     # Write the part up to the first full-stop to STDOUT, hopefully a Built-In shell command rather than a new shell that echo may give
done | sort -u                # Process the STDOUT of the loop to give unique entries

You may, of course, need to add more around it to carry on with the processing.

Does this help you?
Robin

Try also

ls *session*log | awk '{sub (/\..*$/, _); T[$0]} END {printf "Business Layer are : "; for (t in T) {printf "%s%s", DL, t; DL=","}; printf RS}'