awk find and print next string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk find and print next string
# 1  
Old 01-24-2014
awk find and print next string

Can I do this in one awk session. Solution I have is poor.

I want to return the number after PID.

Code:
echo "Start: 12345 is used by PID:11111 username" | awk -F: '{print $3}' | awk '{print $1}'

# 2  
Old 01-24-2014
One way

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk -F":" '/PID/{print $2}' RS=" "
11111

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk  'gsub(/.*PID:|username/,x)'
11111

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk  '{split($6,A,":");print A[2]}'
11111

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-24-2014
Nice solution. Thanks.

I understand that RS is setting a new line but why is username not printed?
# 4  
Old 01-24-2014
Sorry I thought you want only PID

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk  'gsub(/.*PID:/,x)'
11111 username

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk  '{split($0,A,"PID:");print A[2]}'
11111 username

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk -F"PID:" '{print $2}'
11111 username

# 5  
Old 01-24-2014
Hello,

Some more approaches.

Code:
echo "Start: 12345 is used by PID:11111 username" | grep -Po  '(?<=PID:).*'
11111 username

Code:
echo "Start: 12345 is used by PID:11111 username" | sed 's/\(.*PID:\)\(.*\)/\2/g'
11111 username


Code:
 echo "Start: 12345 is used by PID:11111 username" | awk 'gsub(/.*:/,X,$0) {print $0}'
11111 username



Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-24-2014
No - your solution was perfect. I didn't want username.

Was just trying to understand why using
Code:
FS = " "

did not return:

Code:
11111
username

# 7  
Old 01-24-2014
Quote:
Originally Posted by u20sr
No - your solution was perfect. I didn't want username.

Was just trying to understand why using
Code:
FS = " "

did not return:

Code:
11111
username


Default FS is space no need of defining it, if you want to use some other field separator such as tab,comma,etc then you have to define it

Code:
$ echo "Start: 12345 is used by PID:11111 username" | awk '{gsub(/.*PID:/,x);print $1 "\n" $2}'
11111
username


$ echo "Start: 12345 is used by PID:11111 username" | awk '{gsub(/.*PID:/,x);print $1 RS $2}'
11111
username

default RS is "\n"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

2. Programming

Find and print number after string in C

I'm trying find and print a number after a specific user passed string in each line of a text file using C (as requested by the powers that be). I've pieced together enough to read the file, find the string and print the line it was found on but I’m not sure where to even start in terms of finding... (3 Replies)
Discussion started by: cgol
3 Replies

3. UNIX for Dummies Questions & Answers

find string and and print another string

i have a file that looks like this ABC123 aaaaaaaaaaaaaaasssssssssssssssffhhh ABC234 EMPTY ABC652 jhfffffffffffffffffffffffffffffffffffkkkkkkkkkkkk i want to grep "EMPTY" and print ABC234 (3 Replies)
Discussion started by: engr.jay
3 Replies

4. Shell Programming and Scripting

Find longest string and print it

Hello all, I need to find the longest string in a select field and print that field. I have tried a few different methods and I always end up one step from where I need to be. Methods thus far: nawk '{if (length($1) > long) long=length($1); if(length($1)==long) print $1}' The above... (6 Replies)
Discussion started by: SEinT
6 Replies

5. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

6. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

7. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

8. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

9. UNIX for Dummies Questions & Answers

AWK...find a string ,if so print it

Hi, I need to find a string, if it finds then I need to print it , otherwise it has to goto next line.... input is====> uid = shashi, india uid ,uid= asia uid= none, uid=india. none ========== output shold be uid = shashi, india uid , uid= asia uid= none, uid=india. none ... (1 Reply)
Discussion started by: hegdeshashi
1 Replies

10. UNIX for Dummies Questions & Answers

Unix find command to print directory and search string

Hi i need to print pathname in which the string present using 'find' command sample output like this Pathname String to be searched ---------- -------------------- /usr/test/myfile get /opt/test/somefile get Thanks in... (4 Replies)
Discussion started by: princein
4 Replies
Login or Register to Ask a Question