grep numbrs from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep numbrs from a file
# 1  
Old 12-27-2007
grep numbrs from a file

Fedora Core 6 Linux, bash shell.
I have text files with maybe 1,000 lines each, each line is all text and ends with a 3 digit number in parenthesis and a period (074.) (095.) (823.) etc. etc. How would I add up all those 3 digit numbers? (sprdsheet prgrm would be nice but I want a command that will do it) At lease I want a command that will get rid of all of the text and the parenthesis'.
Any help or pointers? Thank you so much!

SAMPLE OF FILE:

Phase.txt
---
Phase 1 of mod release new (034.)
Phase 1A of mod release old (059.)
Misc mod (100.)
Phase 2 of mod release new (599.)
Phase 2A of mod relase old (188.)
.
.
.
.
Phase 950 of mod relase new (176.)
Phase 950A of mod release old (090.)
---

I just want to add up all the (.) numbers.

Last edited by ajp7701; 12-27-2007 at 06:32 PM..
# 2  
Old 12-27-2007
awk '/[.]\)$/{
gsub("\)$","", $NF)
gsub("^\(","", $NF)
a+=$NF
}
END{
print a
}' <filename>
# 3  
Old 12-27-2007
thx!!

WOW thank you! I'll post on here more often! Another question, i'll start a new post for it but I have a list of hex numbers that I want converted to decimal. It might have 500 lines in it or so... example

hexfile.txt
---
3EFF
32E1
A10B
10AB
.
.
.
.
12D1
---


can these values be converted with a shell script?

PS I think I want to donate....can I use paypal? Would it be an insult if it were just a few dollars?
# 4  
Old 12-28-2007
$ cat hex.txt
3EFF
32E1
A10B
10AB

$ cat hex.txt | while read hexn
> do
> let x=0x$hexn
> echo "Hex=$hexn,Decimal=$x"
> done
Hex=3EFF,Decimal=16127
Hex=32E1,Decimal=13025
Hex=A10B,Decimal=41227
Hex=10AB,Decimal=4267

And the donate link for UNIX.com is The UNIX Forums - PayPal Donate

Regards,
Jadu
# 5  
Old 01-02-2008
to awk

in my original question, phase.txt had the numbers in parentheses....come to find out just the example does. the real file does not have the numbers in parenthesis. It looks more like this:

SAMPLE OF FILE:

Phase.txt
---
Phase 1 of mod release new 034.
Phase 1A of mod release old 059.
Misc mod 100.
Phase 2 of mod release new 599.
Phase 2A of mod relase old 188.
.
.
.
.
Phase 950 of mod relase new 176.
Phase 950A of mod release old 090.
---

So what AWK command would I use to add all those numbers?
sorry for the mistake on my part...

Last edited by ajp7701; 01-02-2008 at 02:45 PM..
# 6  
Old 01-02-2008
Quote:
Originally Posted by ajp7701
WOW thank you! I'll post on here more often! Another question, i'll start a new post for it but I have a list of hex numbers that I want converted to decimal. It might have 500 lines in it or so... example

hexfile.txt
---
3EFF
32E1
A10B
10AB
.
.
.
.
12D1
---


can these values be converted with a shell script?

PS I think I want to donate....can I use paypal? Would it be an insult if it were just a few dollars?
nawk -f hex2dec.awk hexfile.txt

hex2dec.awk:
Code:
BEGIN {
    for (i = 0; i < 10; i++)
      hex[i] = i
    hex["a"] = hex["A"] = 10
    hex["b"] = hex["B"] = 11
    hex["c"] = hex["C"] = 12
    hex["D"] = hex["d"] = 13
    hex["e"] = hex["E"] = 14
    hex["f"] = hex["F"] = 15
  }

  function dehex(h,  i,x) {
    for (i = 1; i <= length(h); i++)
      x = x*16 + hex[substr(h, i, 1)]
    return x
  }

{
 print dehex($0)
}

# 7  
Old 01-02-2008
also..

I also need to add the numbers in this file...
SAMPLE OF FILE (500lines):
list.txt
Week end:009.
Week start:137.
Week end:008.
Week start:211.
Misc weeks:309.
Other text lines:767.
.
.
.
Week end:105.
Week start:198.


AWK after the colon, get the numbers, add them. Colon is only used once per line. HOW? :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

4. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

5. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

6. Shell Programming and Scripting

Shell Script to grep Job File name and Log File name from crontab -l

Hello, I am new to shell scripting. I need to write a shell script where i can grep the name of file ie. .sh file and log file from crontab -l. #51 18 * * * /home/oracle/refresh/refresh_ug634.sh > /home/oracle/refresh/refresh_ug634.sh.log 2>&1 #40 17 * * * /home/oracle/refresh/refresh_ux634.sh... (1 Reply)
Discussion started by: guptra
1 Replies

7. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

8. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

9. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

10. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies
Login or Register to Ask a Question