How do I pull a substring out of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I pull a substring out of a file?
# 1  
Old 07-07-2009
How do I pull a substring out of a file?

I'm new to shell scripting and am trying to write a small script that pulls a substring out of a file that has a few lines of text in it. The file will eventually have a lot of text, I just put a few lines in it for testing purposes.

Anyway, this is what I have so far...

Code:
#!/bin/ksh
 
top > testlog.txt
df -h >> testlog.txt
 
freeMem=`grep ",.*free mem," testlog.txt`
home=`grep "home" testlog.txt`

echo $freeMem
echo $home

as of now, it displays the entire line that the grep's return. I want to display just a substring out of each line that's returned from the two greps.

I've tried doing it two ways, but can't get it to work. Here's an example using my freeMem var:

Way 1: echo ${freeMem:22:2}
get an error saying something like bad substitution

Way 2: freeMem=`grep -o ",.*free mem," testlog.txt`
just doesn't work for me, says I can't us -o, but google says I should be able to.

Here's an example of the line that's in testlog.txt that the grep would return:

Memory: 16G phys mem, 11G free mem, 4103M swap, 4103M free swap

I want to display only the substring "11G".

Thanks in advance for any help.
# 2  
Old 07-07-2009
Code:
echo 'Memory: 16G phys mem, 11G free mem, 4103M swap, 4103M free swap' | nawk '$1~ "^Memory"{print $5}'

# 3  
Old 07-07-2009
thank you so much, works perfectly like this...

echo `grep ",.*free mem," testlog.txt` | nawk '$1~ "^Memory"{print $5}'

I'll have to learn nawk and awk
# 4  
Old 07-07-2009
Quote:
Originally Posted by enator45
thank you so much, works perfectly like this...

echo `grep ",.*free mem," testlog.txt` | nawk '$1~ "^Memory"{print $5}'

I'll have to learn nawk and awk
no need for echo AND grep:
Code:
nawk '/free mem,/ && $1~ "^Memory"{print $5}' testlog.txt

# 5  
Old 07-07-2009
even better. thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to pull hashes out of large text file

I am attempting to write a script that will pull out NTLM hashes from a text file that contains about 500,000 lines of data. Not all accounts contain hashes and I only need the ones that do contain hashes. Here is a sample of what the data looks like: There are thousands of other lines in... (6 Replies)
Discussion started by: chango77747
6 Replies

2. Shell Programming and Scripting

Trying to pull a variable out of an xml file...

Hello. I'm new to *ix and am trying to pull a variable or two from an xml document. The document is in the format: <name>7_3(A).mov</name> <description>Some description from a file</description> <updatename>7_3_A.mov</updatename> <updatepath>Dailies Released</updatepath> ... (3 Replies)
Discussion started by: Renfield
3 Replies

3. Shell Programming and Scripting

Easy way to pull a paragraph from a text file?

I have a collection of text files that comprise a mailing list archive. I grep them to find an email that interests me, then open the file in a text editor to find the surrounding paragraph of text. Is there an easy way to do this from the shell instead? (2 Replies)
Discussion started by: CRGreathouse
2 Replies

4. UNIX Desktop Questions & Answers

How to find that has a string and pull the record out of that file

Hello Experts !!! Have some trouble finding a solution for the problem mentioned below. Please help. Thanks, Lee. I have set of input files as below File1 MCMCNDD77 20100903:12:36:50 323-2322 BAC,MRP,NWER CKDJKJK838838 234 ... (4 Replies)
Discussion started by: OMLEELA
4 Replies

5. Shell Programming and Scripting

Please do help: Perl Script to pull out rows from a CSV file

I have CSV file that contains data in the format as shown below: ABC, 67, 56, 67, 78, 89, 76, 55 PDR, 85, 83, 83, 72, 82, 89, 83 MPG, 86, 53, 54, 65, 23, 54, 75 .. .. .. .. I want to create a script that will pull out the rows from the above sheet and paste it into another CSV file.... (12 Replies)
Discussion started by: pankajusc
12 Replies

6. Shell Programming and Scripting

How pull the Data from the file and should be mailed

Hi All, I need to pull the data from a file from 2 set of directories & should be mailed. I have a code , it is failing for some reason. Do somebody correct me where my script is going wrong. Directories: 1st Set of Directory /176_PS/Transactions/**/syslog.log ** : prd1 prd2... (2 Replies)
Discussion started by: raghunsi
2 Replies

7. Shell Programming and Scripting

pull range of dates/times and put into new file

Need to pull from a range of dates/times (ex. 6:00 AM March 3 through 6:00 AM March 4) from a folder and put those file names in a new file to process later. Dates would not be hard dates but dates from the folder directory, how would I do that? (9 Replies)
Discussion started by: freddie999
9 Replies

8. Shell Programming and Scripting

Need script to pull multiple field from log file

I am hoping to get some help with a script to pull certain fields from a log file. User update (xx6xxx P) rpt (yy6yyy B) 2010/01/20 21:36:01.298 Remote client forward start streamid 85af 2010/01/20 21:36:01.307 rpt2 (ZZ6ZZZ G) rpt1 (YY6YYY B) urcall (CQCQCQ ) mycall (W1AW) user... (5 Replies)
Discussion started by: TedSD
5 Replies

9. UNIX for Dummies Questions & Answers

Using awk (or whatever) to pull and append data in a new file

One of the fortunate things about posting in a "Dummies" forum is you probably aren't expecting a lot of out me... I'm trying to pull fields from two lines in the same file(s), and then append them together in a new file. So...I get a nice line-by-line of the first bit of data I'm looking... (6 Replies)
Discussion started by: Milano_EH3
6 Replies

10. UNIX for Dummies Questions & Answers

Pull a file from a remote server through a shell script

Hi, I am writing a shell script to pull a file from a remote server (Let say its a windows based remote server). One of my criteria is to pull a file only if it is not empty. We have done a similar script to push a file from our end to a remote server and before pushing it we check for the... (2 Replies)
Discussion started by: sashankkrk
2 Replies
Login or Register to Ask a Question