How can I extract a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I extract a string?
# 1  
Old 09-05-2011
Question How can I extract a string?

Hi all,

This might be a very small question..

Code:
df -h . | grep %
Filesystem            Size  Used Avail Use% Mounted on
                      4.0G  3.4G  356M  91% /t3appsoftware

How can I extract the string appended with %. The output should be "91" here.

" I have a query here for this.

Code:
df -h .| awk '/[0-8]%/' |awk '{print $(NF-1)}' | cut -c 1,2

"

=>
Code:
df -h .| awk '/[0-8]%/' |awk '{print $(NF-1)}' | cut -c 1,2
91

But looks like a bit confusing..I expect a better query command then the above and also how can I use only one awk instead of two..? Please post any useful links about shell programming. I'm using the Korn shell.

Thanks in advance..

Last edited by pludi; 09-05-2011 at 03:30 AM..
# 2  
Old 09-05-2011
Code:
df -h . | sed -rn '$ s/.* ([0-9]+)%.*/\1/p'

Change the regex to BRE if your sed doesn't have -r option.
# 3  
Old 09-05-2011
Code:
 
df -h . | nawk ' $4~/%/ {print substr($4,length($4)-2,length($4-1))}'

---------- Post updated at 11:23 AM ---------- Previous update was at 11:21 AM ----------

I checked for $4, you can use $(NF-1)
# 4  
Old 09-05-2011
Hi,

Code:
df -h . | nawk ' $4~/%/ {print substr($4,length($4)-2,length($4-1))}'

nawk - do we have this command? Or its a typo and just simply awk.

Throwing an error when I execute the above.

---------- Post updated at 01:00 AM ---------- Previous update was at 12:58 AM ----------

=>
Code:
df -h . | awk ' $4~/%/ {print substr($4,length($4)-2,length($4-1))}'
91

thanks.. Able to excute Smilie

Moderator's Comments:
Mod Comment Please use [CODE] tags when posting command lines, etc.

Last edited by pludi; 09-05-2011 at 03:31 AM..
# 5  
Old 09-05-2011
we have awk, nawk, gawk

I am in solaris, so i always use nawk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract every repeated string between two specific string?

Hello guys, I have problem with hpux shell script. I have one big text file that contains like SOH bla bla bla bla bla bla ETX SOH bla bla bla ETX SOH bla bla bla ETX What I need to do is save first SOH*BLA into file1.txt, save second SOH*BLA into file2.txt and so on.... (17 Replies)
Discussion started by: sembii
17 Replies

2. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Extract a string from another string in UNIX

I have a string string="Please have a nice day and sleep well Replace_12123_31233_32134_12342 Good day" How do i replace "Replace_12123_31233_32134_1234" in the above string.?? Please help. Regards, Qwerty (3 Replies)
Discussion started by: qwertyu
3 Replies

5. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

extract a string within a string using a pattern

hi all, i have a file name using the following pattern: PREFIX: AR SOURCE: LEGACY DATETIME: YYYYMMDD_HH24MISS SUFFIX: .txt sample filename: AR_LEGACY_20101104_105500.txt i want to extract the source which is LEGACY in this case. how do i do this using shell? thanks. (4 Replies)
Discussion started by: adshocker
4 Replies

8. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

9. Shell Programming and Scripting

extract a sub string from a main string

i need a shell program to extract a substring from a main string.. for eg:- main string is madhu.. sub string is mad o/p:- be mad. try to solve this one (5 Replies)
Discussion started by: madhu.it
5 Replies

10. UNIX for Dummies Questions & Answers

How to extract a portion of a string from the whole string

How to extract a portion of a string from a full string using unix. For example: Say source string is = "req92374923.log" I want only the numeric portion of the string say "92374923" how to do that in Unix. (2 Replies)
Discussion started by: ds_sastry
2 Replies
Login or Register to Ask a Question