Help on grep syntax in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on grep syntax in UNIX
# 1  
Old 11-29-2017
Help on grep syntax in UNIX

Dear Team

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

Output -

Code:
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

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-29-2017 at 06:25 AM.. Reason: Added CODE tags.
# 2  
Old 11-29-2017
How can your output be
Code:
lxserver01
lxserver02
lxserver03

When you've clearly ran
Code:
echo Session used server are 'grep -i pid|grep  .. blah blah...

Please state your problem legibly, using code tags, please.
# 3  
Old 11-29-2017
Hi Scott,

Please find my code...Pls help.

Code:
#!/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


Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags as required by forum rules!

Last edited by RudiC; 11-29-2017 at 06:27 AM.. Reason: Changed ICODE to CODE tags.
# 4  
Old 11-29-2017
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 greps 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.
# 5  
Old 11-29-2017
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:
Code:
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!)
# 6  
Old 11-29-2017
Do you need all the detail provided by the ls -l? It might be easier to write a bit like this:-
Code:
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

Last edited by rbatte1; 11-29-2017 at 07:50 AM.. Reason: Comment about using "printf" over "echo" in the loop
# 7  
Old 11-29-2017
Try also
Code:
ls *session*log | awk '{sub (/\..*$/, _); T[$0]} END {printf "Business Layer are : "; for (t in T) {printf "%s%s", DL, t; DL=","}; printf RS}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

FIND and GREP syntax

I have a question to this command find . -type f -name ".*txt" -exec grep "text" {}\. The find command will locate a file name with the extension of txt once per round and find the word "text" in the content of the file or the find command will locate all the file names with the extension of... (2 Replies)
Discussion started by: TestKing
2 Replies

2. Shell Programming and Scripting

Grep syntax print after certain character

My current code is: user@ubuntu:~/Desktop$ grep -e "\(packaged by\)\|\(employee\)\|\(file name\)\|\(Total Data (MB) Read\)\|\(Begin Time\)" log.txt packaged by = Ron Mexico employee = Michael Vick file name = Mike_Vick_2011.bat Total Data (MB) Read: 11.82 Begin Time: 6/13/2011... (8 Replies)
Discussion started by: chipperuga
8 Replies

3. UNIX for Dummies Questions & Answers

Find/Grep Syntax Question

Hi Folks, I am trying to dig through about 100 directories that have 1 or 2 .jpg images stored in each. I want to copy the .jpg to another file in the root directory. Really my ultimate goal is not to have to dig down into each directory to copy the images individually. I thought I could use a... (2 Replies)
Discussion started by: alpinescott
2 Replies

4. Shell Programming and Scripting

Perl Grep Error - Possible Syntax?

Alrighty, I'm trying to get a perl script going to search through a bunch of files for me and compile it to a single location. I am currently having troubles on just getting the grep to work. Here is what I currently have: #!/usr/bin/perl open (LOG, "errors.txt") or die ("Unable to open... (2 Replies)
Discussion started by: adelsin
2 Replies

5. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

6. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies

7. UNIX for Dummies Questions & Answers

Syntax Help | unix | grep | regular expression | repetition

Hello, This is my first post so, Hello World! Anyways, I'm learning how to use unix and its quickly become apparent that a strong foundation in regular expressions will make things easier. I'm not sure if my syntax is messing things up or my logic is messing things up. ps -e | grep... (4 Replies)
Discussion started by: MykC
4 Replies

8. BSD

proper syntax of grep command

I'm learning UNIX on my mac (BSD), using a manual. I'm trying to figure out the grep command, and am getting something wrong. I've opened one of my files in NeoOffice and am looking for a string, the phrase 'I am writing.' I've been to some sites to get the proper syntax, and from what I can see... (5 Replies)
Discussion started by: Straitsfan
5 Replies

9. Shell Programming and Scripting

grep syntax for this...

I wanna grep for a pattern logs 1 2 & 3 within a folder containing 100 logs grep "test" /folder/log1 /folder/log2 /folder/log3 The above command will work fine but is there any command like grep "test" /folder/log1, log2, log3 or something similar (4 Replies)
Discussion started by: roshanjain2
4 Replies

10. Shell Programming and Scripting

Need help with the syntax using awk+grep

Hi, I need to extract information from a 4 GB file based on the following conditions: 1) Check for the presence of a set of account numbers Each account number is present along with other information within a PAGESTART and PAGEEND. The file looks like this: PAGESTART ACCOUNT NO 123... (6 Replies)
Discussion started by: kthri
6 Replies
Login or Register to Ask a Question