Cut string from a line into a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut string from a line into a variable
# 1  
Old 03-04-2010
Cut string from a line into a variable

Hi,
I am working on a ksh script and I´m stuck on the following:
I have to get the pthread_id from a procstack file for a particular tid#.
---------- tid# 1274057 (pthread ID: 1800) ----------
---------- tid# 1736913 (pthread ID: 4019) ----------
---------- tid# 1478705 (pthread ID: 4269) ----------
---------- tid# 1876087 (pthread ID: 4465) ----------
---------- tid# 1912849 (pthread ID: 3086) ----------
---------- tid# 425991 (pthread ID: 2828) ----------
---------- tid# 1954017 (pthread ID: 1543) ----------
---------- tid# 2064419 (pthread ID: 1286) ----------
---------- tid# 1491095 (pthread ID: 1029) ----------
---------- tid# 2265101 (pthread ID: 772) ----------
---------- tid# 418027 (pthread ID: 515) ----------
---------- tid# 1867819 (pthread ID: 258) ----------
---------- tid# 2281679 (pthread ID: 1) ----------
---------- tid# 1851497 (pthread ID: 2571) ----------
---------- tid# 2199627 (pthread ID: 2314) ----------
---------- tid# 450699 (pthread ID: 2057) ----------

I can grep the output file by "tid#" or by the tid# number itself, but what I would like to obtain is the pthread ID. Lets say, I have tid# 1876087 to grep by.

Variable pthread identifies currently the whole line that contains my $TID:

echo pthread=`cat "${OM}_procstack_${PID}.txt" | grep $TID`
---------- tid# 1876087 (pthread ID: 4465) ----------

Can somebody help me to cut the 4465 from this line? I would like to put it into a variable so I can use it.

---------- tid# 1876087 (pthread ID: 4465) ----------

Both numbers on the line (tid# and pthread ID) can vary in length, and I can´t just awk and call it $6 because I do not want the ')' after it. Also, I think you awk files, not lines nor variables, right?

I do not want *everything from ':' to ')' * as there are a varying number of spaces, so I´m not sure how to go about this. I am sure one of you is going to make this sound so easy-peasy in a minute.. Smilie/

Thanks in advance!
T.
# 2  
Old 03-04-2010
Quote:
echo pthread=`cat "${OM}_procstack_${PID}.txt" | grep $TID`
This is yet another Useless Use of Cat.

Creative use of the field separator can help get you what you want without resorting to external commands like cut, sed, and grep:
Code:
$ IFS=" ()" read A B C D E F G H < data
$ echo "A='$A'" "B='$B'" "C='$C'" "D='$D'" "E='$E'" "F='$F'" "G='$G'" "H='$H'"
A='----------' B='tid#' C='1274057' D='pthread' E='ID:' F='1800' G='----------' H=''
$

# 3  
Old 03-04-2010
Code:
awk -F'[ )]' -v tid="$TID" '$3==tid{print $6}' "${OM}_procstack_${PID}.txt"


Last edited by alister; 03-04-2010 at 11:30 AM..
# 4  
Old 03-04-2010
Code:
grep "tid# 1876087" yourfile | sed 's/.*ID: //g' | sed -e 's/\(^[0-9].*\)).*/\1/g'


Last edited by Franklin52; 03-05-2010 at 02:56 AM.. Reason: Please use code tags!
# 5  
Old 03-04-2010
Hi, jostber:

Quote:
Originally Posted by jostber
grep "tid# 1876087" yourfile | sed 's/.*ID: //g' | sed -e 's/\(^[0-9].*\)).*/\1/g'
Using plain grep to filter sed input isn't necessary. Also, you can execute multiple commands with a single sed invocation. A translation of your pipeline:
Code:
sed '/tid# 1876087/!d; s/.*ID: //g; s/\(^[0-9].*\)).*/\1/g' yourfile

Regards,
Alister

Last edited by alister; 03-04-2010 at 01:16 PM..
# 6  
Old 03-04-2010
sample is the your datafile

Code:
cat sample | cut -f2 -d':' |cut -f1 -d")"


Last edited by radoulov; 03-04-2010 at 03:21 PM.. Reason: Please use code tags!
# 7  
Old 03-04-2010
Quote:
Originally Posted by phoenix_nebula
sample is the your datafile

Code:
cat sample | cut -f2 -d':' |cut -f1 -d")"

Yet another Useless Use Of Cat for the collection Smilie A version without the blatant waste:
Code:
cut -f2 -d':' < sample | cut -f1 -d")"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Skip to next line if the variable is string

Hi I have the follwoing requirement I have a file as follows: # cat priy yyy.poweroff_cmd = /sbin/poweroff hhh.powersave-nap = 1 When this file is provided as input, I first used "awk" command and saved variables present after "=" replace=$line replace1=`echo $line | awk -F "="... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

2. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

3. Shell Programming and Scripting

Cut line up to a string

hi all In my bash script I want to cut a line up to a specific string and keep the rest of it but only up to a ".How can I do that?I imagine something with sed.. Let's say my line is: Jennifer Jones (student) "id:376765748587/7465674775" NewYork and i only want to keep: ... (9 Replies)
Discussion started by: vlm
9 Replies

4. UNIX for Dummies Questions & Answers

How to cut a string from a variable

I want to cut column 1 to 5 echo $somevariable | cut -c 1-5 not working (6 Replies)
Discussion started by: ezee
6 Replies

5. Shell Programming and Scripting

How to cut a string from a variable

hello, i need to cut a string in unix but not from file ,, from variable, i searched on special line , and i need a specific data from this line , i get it on variable, how can i cut data that i need ??? merci (2 Replies)
Discussion started by: Reham.Donia
2 Replies

6. Shell Programming and Scripting

Read Line and sent as variable for each string

Hi , I want to read below output, lets called it output1.txt and each string for every line will be declare as a variables. This is the input file 196 server_a server_unix_2 FW 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd ... (2 Replies)
Discussion started by: sQew
2 Replies

7. Shell Programming and Scripting

Line/Variable Editing for Awk sed Cut

Hello, i have a file, i open the file and read the line, i want to get the first item in the csv file and also teh third+6 item and wirte it to a new csv file. only problem is that using echo it takes TOO LONG: please help a newbie. below is my code: WorkingDir=$1 FileName=`cut -d ',' -f... (2 Replies)
Discussion started by: limamichelle
2 Replies

8. Shell Programming and Scripting

cut a string in a textfile line per line

i need to cut the string in a textfile but each line has a specific way of cutting it (different lengths) i have a for loop that gets the string line per line, then each line has to be compared: for x in `cat tmp2.txt`; do if; then echo 'BAC' elif ... (6 Replies)
Discussion started by: izuma
6 Replies

9. Shell Programming and Scripting

To cut end string from line

HI, I want to cut end string from line. e.g. i have following input line /users/home/test.txt I want to get end string 'test.txt' from above line and length of that end string will change and it always start after '/'. Thanks, Visu (7 Replies)
Discussion started by: visu
7 Replies

10. UNIX for Dummies Questions & Answers

how to cut a string from a variable

Hello All, I have a string type variable called "FULLSTR". This variable has a value of "path=/type/source/logs". I need to cut the characters after the character "=" to end of the string. Example: the new variable "MATCHSTR" should have value like this... "/type/source/logs" So... (2 Replies)
Discussion started by: kjaisan
2 Replies
Login or Register to Ask a Question