How to extract portion of a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract portion of a string?
# 8  
Old 04-05-2012
Quote:
Originally Posted by superHonda123
Is it possible to extract the string based on a keyword such as: df0 ?
Code:
awk -v FS="[ =]" '/df0/ { print $3 }' inputfile | xargs -L1 basename

or if you want to pass the value in from a script:
Code:
srch="df0";
awk -v FS="[ =]" -vval=$srch '$0 ~ val { print $3 }' inputfile | xargs -L1 basename


Last edited by CarloM; 04-05-2012 at 05:37 AM..
This User Gave Thanks to CarloM For This Post:
# 9  
Old 04-05-2012
Code:
awk -F"[ /]" '{print $(NF-2)}' inputfile

This User Gave Thanks to ctsgnb For This Post:
# 10  
Old 04-05-2012
Code:
awk '{$1=$1}NF=NF>2' RS=/ infile

Some awks:
Code:
awk 'NF=NF>2' RS=/ infile


Last edited by Scrutinizer; 04-05-2012 at 06:11 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 11  
Old 04-05-2012
Nice tricky last one ! Thx !
# 12  
Old 04-06-2012
Hi All,

Great.

Thanks for your responses and help.

Let me test it tomorrow and let you know.


- Peter

---------- Post updated 04-06-12 at 05:00 AM ---------- Previous update was 04-05-12 at 06:59 AM ----------

Hi All,

Would like to add that I'm using Solaris system as shown below.

Code:
-> uname -a
-> SunOS 5.10 Generic_144488-08 sun4u sparc SUNW,SPARC-Enterprise

I've tried most the given awk commands, but seems to be having some issues, see below.

Code:
root # awk -v FS="[ =]" '/df0/ { print $3 }' test1.txt  | xargs -L1 basename
awk: syntax error near line 1
awk: bailing out near line 1

---
root # srch="df0";
root # awk -v FS="[ =]" -vval=$srch '$0 ~ val { print $3 }' test1.txt | xargs -L1 basename
awk: syntax error near line 1
awk: bailing out near line 1

---
root # awk -F"[ /]" '{print $(NF-2)}' test1.txt
awk: trying to access field -1
 record number 1

---

Did I miss out anything?

Appreciate for your advise.

Thanks.


- Peter

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 04-06-2012 at 07:17 AM..
# 13  
Old 04-06-2012
Don't use /usr/bin/awk on Solaris. Use nawk or /usr/xpg4/bin/awk instead.
This User Gave Thanks to radoulov For This Post:
# 14  
Old 04-06-2012
Hi All,

Thanks for your responses.

It's working as expected. Great.

Appreciate for your help.



- Peter
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a portion of string from each line in Linux

Hi I have to extract the destination path information from each record the file is of variable length so I will not be able to use the print command.The search should start on variable "destinationPath" and it should end at immediate "," also the first field has to be printed Input File:... (7 Replies)
Discussion started by: rkakitapalli
7 Replies

2. Shell Programming and Scripting

Extract portion of data

Hi Gurus, I need some help in extracting some of these information and massage it into the desired output as shown below. I need to extract the last row with the header in below sample which is usually the most recent date, for example: 2012-06-01 142356 mb 519 -219406 mb 1 ... (9 Replies)
Discussion started by: superHonda123
9 Replies

3. Shell Programming and Scripting

Extract portion of log info based on specific word

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract a specific wording for example: "A tool used by tp produced warnings" from my below log data, but could not find a way to solve it. My intention is, if the log contain the word: "A tool used by tp produced... (9 Replies)
Discussion started by: superHonda123
9 Replies

4. Shell Programming and Scripting

How to extract a text portion from a file

Can some one help me with shell script to extract a text block between two known strings. The given input file is as below: Name: abs Some tesxt.... Some tesxt.... Some tesxt.... end of text Name: xyz Some tesxt.... Some tesxt.... Some tesxt.... end of text Name: efg Some... (5 Replies)
Discussion started by: ejazs0
5 Replies

5. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

6. Shell Programming and Scripting

extract string portion using sed

Hi All I have 3 files as listed below and highlighted in bold the portions of the filenames I need to extract: TOS_TABIN218_20090323.200903231830 TOS_TABIN219_1_20090323.200903231830 TOS_TABIN219_2_20090323.200903231830 I tried source_tabin_name=`echo $fname | sed 's/_.*//'` but I... (6 Replies)
Discussion started by: santam
6 Replies

7. UNIX for Dummies Questions & Answers

How to extract a portion of text from a log file

I am using Unix on Mac OS X 10.5.6. I am trying to extract the last entry of a log (text) file. As seen below, each log entry looks like the following (date and time change with each log entry): I want the script to extract everything quoted above, including the "===" dividers. ... (2 Replies)
Discussion started by: atilano
2 Replies

8. Shell Programming and Scripting

extract date portion from file

Hi, I have a file where there is a date field (single line variable length file) how to extract just the date portion from it the position of date field may vary anywhere in the line but will always have the format mm-dd-yyyy for eg . xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx (5 Replies)
Discussion started by: misenkiser
5 Replies

9. Shell Programming and Scripting

extract a portion of log file

hello, I want to grep the log file according to time and get the portion of log from one particular time to other. I can grep for individual lines by time but how should I print lines continuously from given start time till end till given end time. Appreciate your ideas, Thanks chandra (8 Replies)
Discussion started by: chandra004
8 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