Printing out pattern in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing out pattern in line
# 1  
Old 11-21-2007
Printing out pattern in line

I've scoured the forum and found similar problems but I can't seem to adapt them to help me with my cause.

This is a two-part question.

I have a multi line file generated by ps | -ef

I need to print out a certain type of pattern. The pattern is part static and part dynamic.

It is a file/s(pattern) that contain/s two letters and three to four numbers and sometimes even letter at the end.

e.g GY1020.def, WN093.def and so on. The files(patterns) are located in different directories (some with very long names) so the result from "ps -ef" cuts off part of the extension (.def) so it reads WN093.d

The output from "ps -ef" looks something like this (last two columns shown below):

/bin/ksh /export/home/user/RUN/DEFFILES/GY/GY1020.def G
/bin/ksh /export/home/user/RUN/DEFFILES/WN/RST/WN093.d
/bin/ksh /export/home/user/VB9530.def /more/text/ 1
/bin/ksh /export/home/user/RUN/DEFFILES/LO/LO6002a.def

I want to print out:
GY1020
WN093
VB9530
LO6002a

alternatively: (appending .def for those that get cut off)
GY1020.def
WN093.def
VB9530.def
LO6002a.def

I know "sed" can do this and search for [A-Z][A-Z][0-9][0-9][0-9] or something similar but I cannot get exactly what I want.

The second part of my question; Is there a way to get "ps" to output ALL data and not just cut the end of as shown above?

Some help with this would be very much appreciated.

Last edited by FK_Daemon; 11-21-2007 at 10:57 AM..
# 2  
Old 11-21-2007
if on Solaris try: '/usr/ucb/ps -awww'
Code:
/usr/ucb/ps -awww | nawk '$2 ~ /[.]def$/ {n=split($2, a, "(/|[.])"); print a[n-1]}'


Last edited by vgersh99; 11-21-2007 at 11:10 AM..
# 3  
Old 11-21-2007
With sed:

Code:
ps -f | sed 's!.*\/\(.*\)\..*!\1!'

with the extension ".def":

Code:
ps -f | sed 's!.*\/\(.*\)\..*!\1.def!'

Regards

Last edited by Franklin52; 11-21-2007 at 12:40 PM..
# 4  
Old 11-23-2007
Thanks for the replies. They helped me a lot.

I thought about matching everything between "/" and "." too, but I was a bit worried that even the "." might get cut off (not to mention that I am no ace when it comes to sed). Hopefully that won't occur. I will look more closely at ps -awww in the future as I run all my scripts on Solaris.

Thanks once again, you guys are awesome.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Need help on pattern matching and printing the same

Hi, I need to match for the pattern '.py' in my file and print the word which contains. For example: cat testfile a b 3 4.py 5 6 a b.py c.py 4 5 6 7 8 1.py 2.py 3 4 5 6 Expected output: 4.py b.py c.py 1.py 2.py (3 Replies)
Discussion started by: Sumanthsv
3 Replies

3. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. UNIX for Dummies Questions & Answers

Printing nth and n+1th line after a pattern match

Hi , I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed :) (6 Replies)
Discussion started by: saj
6 Replies

6. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

Printing previous line based on pattern using sed

Hi, I have a written a shell script to get the previous line based on the pattern. For example if a file has below lines: ---------------------------------------------- #UNBLOCK_As _per #As per 205.162.42.92 #BLOCK_As_per #----------------------- #input checks abc.com... (5 Replies)
Discussion started by: Anjan1
5 Replies

8. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies

9. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

10. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies
Login or Register to Ask a Question