Stripping all content but an integer


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Stripping all content but an integer
# 1  
Old 07-20-2007
Stripping all content but an integer

Hello! I have content in a log file that consists of a lot of spaces before and after a 3 digit integer which I need to strip out before I can use the file. The number of digits can change. When I had my logic in a 'for' loop and could output into another file, it was fine. but it turns out that I will only have 1 line now and don't need the for loop. I have tried a lot of different logic for grepping using the 'x' switch and it didn't work outside of the loop. I tried the E switch as well. This is what I had that worked inside of a for loop, any ideas on how to strip the spaces from the integer? I tried taking just that one line below with the grep, I tried using 'cat' instead of 'echo', nothing worked:

SQL_COUNT=`cat ${LOGPATH}/sql_count.log`
NUM_LOG=${LOGPATH}/num.log

for x in ${SQL_COUNT}
do
echo $x | grep -x '[0-9]*'
done > ${NUM_LOG}
# 2  
Old 07-20-2007
Use tr:
Code:
 echo "       1234567         " | tr -dc '[0-9]'

Or in your case try
Code:
cat filename | tr -dc '[0-9]'

# 3  
Old 07-20-2007
Stripping all content but an integer

Worked perfectly, thanks!
# 4  
Old 07-20-2007
The solution:
Code:
... | tr -dc '[0-9]'

also removed he carriage return at the end of each line.

Here is another solution, keeping the carriage return:
Code:
echo "       1234567         " | sed 's/ //g'
sed 's/ //g' input_file

# 5  
Old 07-20-2007
A cruel way ! Smilie
Code:
echo "       1234567         " | awk '{ print $0 + 0 }'

# 6  
Old 07-20-2007
Yes, I didn't realize that I didn't have the carriage return, then again, I'm too new at this to realize that when my ID prompt listed right after my number that it meant I didn't have a carriage return Smilie. Thanks!

I'm working with a ksh script on a windows box calling a sendmail bat file, now if I can only get it to send me mail! Smilie At least I have the trimmed number to work with, maybe there's something with how I'm manipulating my code - yesterday it worked and I have changed things today...

Thanks again,
# 7  
Old 07-20-2007
Quote:
Originally Posted by jim mcnamara
Use tr:
Code:
cat filename | tr -dc '[0-9]'

Code:
tr -dc '[0-9]' < filename

 
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 compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

2. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

3. Solaris

Diffrence between stripping and mirroring

Hi All, What is the difference between stripping and mirroring? Thanks, Rafi. (3 Replies)
Discussion started by: rafidba.alvi
3 Replies

4. UNIX for Dummies Questions & Answers

Stripping down binaries

Hello, I am the CEO of Grand Tech Corporation. We are launching Linux NT and forgive me, but I do not know how to strip binaries down in Mandriva Linux. Can someone tell me a way to?:b: (2 Replies)
Discussion started by: Linux NT
2 Replies

5. Shell Programming and Scripting

stripping a variable down

I have a variable that has an absolute path for a file on my computer. This dynamically changes. Is there a way I can assign two new variables from that one? variable: /Users/keith/Desktop/test/file.mov 1) filename - no path or extention ....so just....file 2) path no filename or... (3 Replies)
Discussion started by: mainegate
3 Replies

6. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies

7. Shell Programming and Scripting

stripping out certain charecters

we are ftping zipped up files from the development server to the production server daily.The files are in this format filename.dat.20061231.12131.gz I have to unzip the file (i can do that with gunzip) and then strip out the timestamp after the .dat extension. I can do something like this ... (4 Replies)
Discussion started by: mervin2006
4 Replies

8. UNIX for Dummies Questions & Answers

stripping server name from path

can anyone help me with stripping an absolute filepath? I did a search on this topic but didnt find anything but maybe I didn't look hard. Anyway, would really appreciate it if anyone could help me. I am a new unix user so this might be a simple issue but I am stuck, don't really understand the use... (4 Replies)
Discussion started by: Ecclesiastes
4 Replies

9. UNIX for Dummies Questions & Answers

stripping last lien off a file

Hi, how can i strip the last line off my file using shell script? Thanks and Regards Vivek.S (3 Replies)
Discussion started by: vivekshankar
3 Replies

10. Shell Programming and Scripting

Stripping date fields

How would I strip a date field? For example, if I have TODAY defined as the following: TODAY=`date +"%m/%d/%y"`, TODAY is assigned the value 11/01/03 on Nov 1, 2003 How would I strip the first 2 bytes? (11)? Thank you in advance for the help. (6 Replies)
Discussion started by: Latha Nair
6 Replies
Login or Register to Ask a Question