How to get a number from a grepped sentence of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get a number from a grepped sentence of a file?
# 1  
Old 08-02-2012
How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number.

For Example:
Code:
#!/bin/ksh
echo "Enter RRS ID: "
read rrs
echo "Enter error number:"
read err
scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/
cat $rrs.*.err.txt | grep "Error $err"

This will give output as follows:

Code:
Error 118173 in ratings for shelf/program ID 823197064: Currency cap is invalid for the rating dated 'Aug  1 2012  3:52PM'; check that currency of this instrument or program matches the currency of the domicile of the issuer.

From this sentence i have to get 823197064 number.

Please advise.Smilie

Last edited by Scrutinizer; 08-02-2012 at 03:29 AM.. Reason: code tags
# 2  
Old 08-02-2012
Code:
 
cat $rrs.*.err.txt | grep "Error $err"

change it to

awk -v err="$err" '/Error/ && $0~err {for(i=1;i<=NF;i++)if($i~/ID/){print $(i+1);next}}' $rrs.*.err.txt

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 08-02-2012
Another one:
Code:
awk '$0~e {if(match($0,/ID [0-9]+/)) print substr($0,RSTART+3,RLENGTH-3)}' e="Error $err" $rrs.*.err.txt

And the output will not have the colon generated by the previous solution.
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 08-02-2012
Thank you all.
It worked..
As i am learner of UNIX, Could you please explain code.

One more question,
If same error come more than one time for different ID numbers,than?/?
In other words grepped result would be more than one line than...???
# 5  
Old 08-02-2012
Then the statements given to you should print one line of output for each matching line of input.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 08-02-2012
If grepped setence is the duplicate, is there any way to remove it?
I have verified for above output it will give desired output for multiple IDs but with \n seperator, shall i able to change it to , seperator?
# 7  
Old 08-02-2012
I'm not quite sure what you mean.

Please show an example of the input that shows the problem and output which shows the result you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I must use first sentence from a file to put into variable

i am having some bash script which must use first sentence of the file. For example i have file which content is: test 213 So I must use word test into my bash script, and put it into variable. I am using a one variable named value value=$(</home/rusher/test.txt) so instead using test.txt... (1 Reply)
Discussion started by: tomislav91
1 Replies

2. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

3. UNIX for Dummies Questions & Answers

Help with if then sentence (string in file)

Hello! I'd like some help with a sentance, this 'if' should take a string from the user, then search my list for that string, now only those lines that string is found should be worked on. I'm new to this, but i'm guessing it's something like this.. #!/bin/bash ... (10 Replies)
Discussion started by: klskl
10 Replies

4. Shell Programming and Scripting

How to receive a specific alphanumeric number from a sentence?

Hi, I am quite new to shell scripting. I am facing challenge in retrieving a specific number from a sentence from the log. the number is random and changes everytime in the log. For example, The number of rows updated in table is: 7000 The number of rows updated in table is: 8000 The... (3 Replies)
Discussion started by: arghadeep adity
3 Replies

5. Shell Programming and Scripting

Replacing grepped text.

Dear Friends, I need your help once again. I have a flat file from which I have grepped something and kept in a separate file for some reason. I want to replace these grepped srtings with the strings in another file. E.g. Actual_file.txt COLOR: 33179 Lakme SEPT2011 Lot No: BR25324 ... (5 Replies)
Discussion started by: anushree.a
5 Replies

6. Shell Programming and Scripting

Puzzle: file name grepped from text file yields "no such file"

Dear all, In a bash script, I grep a filename from an UTF8 encoded file: LIST=`grep ^source $FILE | tr "\t" " " | cut -d " " -f 2 | sed -e 's,~,\$HOME,g'` The result is # echo $LIST $HOME/.mail_aliases_seminaire_MMMG Then I try to access it: #ls $LIST ls: cannot access... (3 Replies)
Discussion started by: josce
3 Replies

7. Shell Programming and Scripting

I have to pass a sentence in a file,

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' > runx ... (1 Reply)
Discussion started by: patilrakesh1984
1 Replies

8. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

9. Shell Programming and Scripting

Append a sentence in each file.

In a directry there are 100 files are present.... How to append a statement like "Anup Das" in each of the file content, in the first line.... without opening the files.... (2 Replies)
Discussion started by: anupdas
2 Replies

10. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies
Login or Register to Ask a Question