Printing the lines using search patterns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Printing the lines using search patterns
# 1  
Old 07-19-2011
Printing the lines using search patterns

Hi all ,

i need an help here.!!!! i have a file that contains /etc/passwd files from some servers.

Quote:
server1
51144:x:1051:401:morris:/export/home/51144:/bin/ksh
51238:x:1051:401:Bryan:/export/home/51238:/bin/ksh
......goes on

server2
51144:x:1051:401:morris:/export/home/51144:/bin/ksh
51238:x:1051:401:Bryan:/export/home/51238:/bin/ksh
root:x:0:0:Super-User:/root:/bin/bash
i need a script which search for presence of a user in the servers.
like if i give 51144 to the script. the should be o/p

Quote:
user:51144
server1
server2
...
...
Please help on this.....
# 2  
Old 07-19-2011
Code:
awk -v FS=":" -v USER="51144" '
BEGIN { print "user:" USER; }
{
        if(NF < 2)              TITLE=$1
        else if($1 == USER)     print TITLE;
}' < data

# 3  
Old 07-20-2011
Or try..
Code:
awk '/51144/{print $1}' RS= inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 07-20-2011
thanks for ur reply Corona688 and michaelrozar17, but i really didnt get what i expected .
ok may be, i will explain little further . i have file thats look like below

Quote:
server1
root:x:0:1:root-/:/sbin/sh
smtp:x:0:0:Mail Daemon User:/:/dev/null
daemon:x:1:1::/:/dev/null
bin:x:2:2::/usr/bin:/dev/null
sys:x:3:3::/:
............
continues..........

server2
root:x:0:1:root-
/:/sbin/shadm:x:4:4:Admin:/var/adm:/dev/null
uucp:x:5:5:uucp Admin:/usr/lib/uucp:/dev/null
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/dev/null
smmsp:x:25:25:SendMail Message Submission Program:/:/bin/false
.......................
.....................

server3
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/dev/null
bin:x:2:2::/usr/bin:/dev/null
like that there are nearly 100 servers , i just want to print the user and the servers where account exists.
Quote:
for eg;
user is root then o/p should be

User:root
server1
server2

if user is nuucp o/p should be

user:nuucp
server2
server3
# 5  
Old 07-20-2011
Something like this..?
Code:
 awk -v usr="root" 'BEGIN{print "User:" usr} $0 ~ usr {print $1}' RS= inputfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Search for patterns on different lines

im using the following code to search a log for entries on two different lines: awk 'BEGIN{count=0} /'"${firstpattern}"'/,/'"${secondpattern}"'/ { print; if ($0 ~ /'"${thirdpattern}"'/){count++}; } END { print count }' data.txt firstpattern="start error log" secondpattern="i am logging the... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

4. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

5. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

6. Shell Programming and Scripting

printing patterns

I have the following file : 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 Write scripts to get the following outputs : A) 1 7 7 1 4 (11 Replies)
Discussion started by: pssooraj72
11 Replies

7. Shell Programming and Scripting

reading lines from a file between two search patterns

Hi, I am new to shell scripting and is working on a script to extract lines from a log file between two time stamps using awk command. After some research I used following command: awk '/01 Oct 2011/{p=1} /10 Oct 2011/{p=0} p' test.log >> tmp.log This works fine. But now i want to... (3 Replies)
Discussion started by: davidtd
3 Replies

8. Shell Programming and Scripting

Printing 10 lines above and below the search string: help needed

Hi, The below code will search a particular string(say false in this case) and return me 10 lines above and below the search string in a file. " awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print("***********************************");print;c=a;}b{r=$ 0}' b=10 a=10 s="false" " ... (5 Replies)
Discussion started by: vimalm22
5 Replies

9. UNIX for Dummies Questions & Answers

printing only the middle word between two patterns

How would I print the word "and" between the words "FOO" and BAR" using sed? My file has three words in it FOO and BAR. I only want the word "and". Thanks every one. (7 Replies)
Discussion started by: tigta09
7 Replies

10. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies
Login or Register to Ask a Question

Featured Tech Videos