Preserve first line and search in others


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve first line and search in others
# 1  
Old 11-14-2009
Preserve first line and search in others

Hi gurus Is possible to preserve first line of text and search in another lines ?

the only way that I think is:

Code:
ps aux | awk '{if (NR==1) {print $0} else if ($11 ~ /sshd/) {print $0}}'

but is there any more elegant way ? thanks
# 2  
Old 11-14-2009
More elegant? Dunno. Shorter? yes Smilie
Code:
ps aux | awk 'NR==1||$11~/sshd/'

# 3  
Old 11-14-2009
Quote:
Originally Posted by wakatana
Hi gurus Is possible to preserve first line of text and search in another lines ?
Hi, I'm not sure if this is more elegant, but it's an alternative:

Code:
lakris@landet:~$ ps aux|head -1;ps aux|grep firefox|grep -v grep
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
lakris    2035 13.9 12.2 344128 125440 ?       Sl   16:42   0:47 /usr/lib/firefox-3.5.5/firefox -new-tab http://art.gnome.org/backgrounds/
lakris@landet:~$

Best regards,
Lakris
# 4  
Old 11-14-2009
Code:
ps aux | egrep "^U|ssh[d]"

Code:
ps aux | sed '1p;/ssh[d]/!d'



---------- Post updated at 08:13 AM ---------- Previous update was at 08:04 AM ----------
Not the same format but..
Code:
ps -fC "sshd"


Last edited by Scrutinizer; 11-14-2009 at 12:21 PM..
# 5  
Old 11-14-2009
thanks Smilie
Code:
ps aux | awk 'NR==1||$11~/sshd/'

is nice Smilie
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 do you preserve the max length of a line after replacing a specific value?

I'm new with scripting and I can't seem to figure out what I should do to get the output that I want. My file content would be below. ID2|ID3 |ID4|ID5 | I'm trying to replace the field of ID3 which has a fixed length of 10 characters, for each entry I have placed on a .txt file... (5 Replies)
Discussion started by: asdfghjkl
5 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

Search: find current line, then search back

Hello. I want to find a line that has "new = 0" in it, then search back based on field $4 () in the current line, and find the first line that has field $4 and "last fetch" Grep or Awk preferred. Here is what the data looks like: 2013-12-12 12:10:30,117 TRACE last fetch: Thu Dec 12... (7 Replies)
Discussion started by: JimBurns
7 Replies

4. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

5. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

6. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

8. Shell Programming and Scripting

Read and copy xml line by line and preserve tab?

I'm trying to read an xml file and copy it line by line to another file and want to preserve the tabs. What i'm trying to do is if I get to a certain line in the xml, I'm going to check to see if the next line is specifically what I want. If it's not, then I want to insert a single line of text... (4 Replies)
Discussion started by: DeuceLee
4 Replies

9. UNIX for Dummies Questions & Answers

query on how to search for a line and read 4th word from that line

Assume I have a text file as below: me con pi ind ken pras ur me con rome ind kent pras urs pintu con mys ind pan pras ki con kit ind sys My requirement, I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome"... (4 Replies)
Discussion started by: jaggesh
4 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question