Extracting character between 2 token - using only first match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting character between 2 token - using only first match
# 1  
Old 04-09-2014
Extracting character between 2 token - using only first match

Hello.

ps -ae return
Quote:
2638 ? 00:00:01 kworker/u8:0
2755 ? 00:00:00 pickup
3111 ? 00:00:00 kworker/u8:2
3127 ? 00:00:00 kwrite
3132 ? 00:00:00 kworker/0:2
3134 ? 00:00:00 kworker/2:2
3141 ? 00:00:00 kworker/0:0
3167 ? 00:00:00 kworker/2:0
3168 ? 00:00:00 kworker/u8:1
3214 ? 00:00:01 acroread
3334 pts/1 00:00:00 ps
I would like that the following command return 3214
Code:
echo " 3214 ?        00:00:01 acroread" | grep -o "[^[:space:]][[:digit:]][^[:space:]]*"

# 2  
Old 04-09-2014
Try grep with extended regex:
Code:
echo " 3214 ?        00:00:01 acroread" | grep -Eo "[[:space:]][[:digit:]]{4}[[:space:]]"
 3214

This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-09-2014
I'm not quite sure I understand. Are you trying to print the first column of the first entry whose third and fourth columns are 00:00:01 and acroread, respectively?

Edit: To add to RudiC's answer:
Code:
echo " 3214 ?        00:00:01 acroread" | grep -Eo "[[:space:]][[:digit:]]{4}[[:space:]]" | tr -d ' '

# 4  
Old 04-09-2014
If you are interested in awk, you may try this one

Code:
$ echo " 3214 ?        00:00:01 acroread" | awk --re-interval '{match($0,/^[[:space:]][[:digit:]]{4}[[:space:]]/,m);print m[0]}'
 3214

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 04-09-2014
Quote:
Originally Posted by blakeoft
I'm not quite sure I understand. Are you trying to print the first column of the first entry whose third and fourth columns are 00:00:01 and acroread, respectively?

Edit: To add to RudiC's answer:
Code:
echo " 3214 ?        00:00:01 acroread" | grep -Eo "[[:space:]][[:digit:]]{4}[[:space:]]" | tr -d ' '

I want to extract >3214< from > 3214 ? 00:00:01 acroread<

---------- Post updated at 17:58 ---------- Previous update was at 17:50 ----------

And this work also :

Code:
echo " 3214 ?        00:00:01 acroread" | sed 's:^ \(.*\) ?.*$:\1:'

Thank you every body for helping.
# 6  
Old 04-09-2014
Longhand using __builtins__, OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
# 3214.sh
ifs_str="$IFS"
IFS="
"
echo "2638 ? 00:00:01 kworker/u8:0
2755 ? 00:00:00 pickup
3111 ? 00:00:00 kworker/u8:2
3127 ? 00:00:00 kwrite
3132 ? 00:00:00 kworker/0:2
3134 ? 00:00:00 kworker/2:2
3141 ? 00:00:00 kworker/0:0
3167 ? 00:00:00 kworker/2:0
3168 ? 00:00:00 kworker/u8:1
3214 ? 00:00:01 acroread
3334 pts/1 00:00:00 ps " > /tmp/3214.txt
line_number=1
while read line
do
	if [ "${line:0:4}" == "3214" ]
	then
		printf "3214" > /tmp/3214.text
		printf "\nLine number=$line_number...\n\n"
	fi
	line_number=$(( $line_number + 1 ))
done < /tmp/3214.txt
cat < /tmp/3214.text
IFS="$ifs_str"
exit 0

Results:-
Code:
Last login: Wed Apr  9 18:22:18 on ttys000
AMIGA:barrywalker~> chmod 755 3214.sh
AMIGA:barrywalker~> ./3214.sh

Line number=10...

3214AMIGA:barrywalker~> _

Also the line number it is on as a bonus too... ;o)
Note: The "3214" without a newline...

(Rigged for silent running for a while.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

sed - extracting the first word only if match

Hello. from a text file, I want to get only the first word ( before blank ) following code= grep -i -e "WORD1" "/path/to/text/file.txt | sed -n 's/WORD1\+//p' | sed -n 's/code=/\1/p' return an error. sed: -e expression #1, char 12: invalid reference \1 on `s' command's RHSFor debugging... (12 Replies)
Discussion started by: jcdole
12 Replies

3. Shell Programming and Scripting

Q:Perl Extracting & Printing Security Token

I have a script which is supposed to log in to my vB account and print my security token, however doesn't seem to work globally. The logging in works perfectly just will not retrieve and print the security token for every forum I log in to. Code Below: #!/usr/bin/perl use LWP::UserAgent; ... (8 Replies)
Discussion started by: AndrewTwain
8 Replies

4. UNIX for Dummies Questions & Answers

extracting character

This time I am trying to extract the number 10 from the following line. This number is subject to change and may be anything from 1 to 3 numerals in length, i.e. 1-999. Hence why I dont want to use 'cut' cmd. The line I am working on is GGSN-MIB::ggsnApnName.10 = STRING: open.internetI have... (7 Replies)
Discussion started by: rob171171
7 Replies

5. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

7. Shell Programming and Scripting

Extracting the last character of a string

How to extract the last character of a string in bash? ---------- Post updated at 03:56 PM ---------- Previous update was at 03:55 PM ---------- Suppose "abcde" is a string. i want to extract the last character i.e. "e". (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

Extracting first character from a user response

I am using the following code in a CShell script to get a yes/no response from the user: echo "" echo -n "Do you want to archive your main level directory? <y> or n: " set main_answer = $< Is there a way to extract the first letter from the user's response and then perhaps convert that... (4 Replies)
Discussion started by: phudgens
4 Replies

9. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

10. UNIX for Dummies Questions & Answers

Extracting lines that match string at certain position

I have a fixed length file in the following format <date><product_code><other data> The file size is huge and I have to extract only the lines that match a certain product code which is of 2 bytes length. I cannot use normal grep since that may give undesirable results. When I search for prod... (5 Replies)
Discussion started by: paruthiveeran
5 Replies
Login or Register to Ask a Question