Problem in getting data from a loop using grep and cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in getting data from a loop using grep and cut
# 1  
Old 09-29-2010
Question Problem in getting data from a loop using grep and cut

The script is following :
Code:
for each_rec in <file_name>
do
  count=`cut -c -2 ${each_rec} | grep "45"`
  echo ${count}
  if [[ ${count} -eq 45 ]] then
    amount=`cut -c 24-35 ${each_rec}`
    echo ${amount}
  else
    echo "failed"
  fi
done

And the file looks like below :

Code:
01000007683+0000013071+001449750.71+001596774.86 2010-09-27201009
02000000219+0000001463+000188697.81+000187069.70 2010-09-27201009
03000000652+0000003367+000212138.84+000847244.61 2010-09-27201009
04000001175+0000005397+000126543.77+000460763.26 2010-09-27201009
05000001890+0000012514+000059183.61+000038238.85 2010-09-27201009
06000000254+0000001149+000004773.04+000000251.77 2010-09-27201009
07000000128+0000000590+000002843.56+000000148.39 2010-09-27201009
08000002728+0000008947+008282517.52+004450313.95 2010-09-27201009
12000000917+0000001974+000555783.18+000819727.45 2010-09-27201009
14000000007+0000000010+000004773.00+000001026.80 2010-09-27201009
15000000298+0000016858+000334163.89+000478644.01 2010-09-27201009
16000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
18000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
27000000078+0000000196+000009205.21+000003151.24 2010-09-27201009
21000000057+0000000219+000039995.49+000039382.41 2010-09-27201009
25000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
30000000116+0000000466+000299833.16+000005895.46 2010-09-27201009
40000002108+0000003477+000311888.91+000219808.01 2010-09-27201009
35000000010+0000000281+000033884.00+000032151.00 2010-09-27201009
28000000336+0000001258+000799387.65+000313825.85 2010-09-27201009
60000000690+0000003421+001010850.57+000564147.45 2010-09-27201009
55000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
51000000615+0000002544+000010538.52+000000597.82 2010-09-27201009
52000000436+0000002738+000010654.19+000000673.83 2010-09-27201009
53000000084+0000000455+000004983.83+000000323.46 2010-09-27201009
09000002856+0000074490+001429055.86+002321155.60 2010-09-27201009
10000000332+0000001322+000006250.50+000000328.37 2010-09-27201009
11000000041+0000000176+000000968.00+000000112.64 2010-09-27201009
45000000010+0000000033+000011481.00+000008827.54 2010-09-27201009


Expected result :

Code:
000011481.00

Result coming as :
Code:
001449750.71
000188697.81
000212138.84
000126543.77
000059183.61
000004773.04
000002843.56
008282517.52
000555783.18
000004773.00
000334163.89
000000000.00
000000000.00
000009205.21
000039995.49
000000000.00
000299833.16
000311888.91
000033884.00
000799387.65
001010850.57
000000000.00
000010538.52
000010654.19
000004983.83
001429055.86
000006250.50
000000968.00
000011481.00

I am trying to fetch the corresponding column, where the first two field matches with 45. But, every time the cut is fetching the whole column.
Please help me on this .



Moderator's Comments:
Mod Comment Please use code tags and indent your code

Last edited by Franklin52; 09-29-2010 at 09:41 AM..
# 2  
Old 09-29-2010
Code:
$ ruby -F'\+' -ane 'puts $F[2] if $F[0][0,2]=="45"' file
000011481.00

# 3  
Old 09-29-2010
Quote:
Originally Posted by mady135
The script is following :
Code:
for each_rec in <file_name>
do
count=`cut -c -2 ${each_rec} | grep "45"`
echo ${count}
if [[ ${count} -eq 45 ]] then
  amount=`cut -c 24-35 ${each_rec}`
  echo ${amount}
else
  echo "failed"
fi
done

And the file looks like below :

Code:
01000007683+0000013071+001449750.71+001596774.86 2010-09-27201009
02000000219+0000001463+000188697.81+000187069.70 2010-09-27201009
03000000652+0000003367+000212138.84+000847244.61 2010-09-27201009
04000001175+0000005397+000126543.77+000460763.26 2010-09-27201009
05000001890+0000012514+000059183.61+000038238.85 2010-09-27201009
06000000254+0000001149+000004773.04+000000251.77 2010-09-27201009
07000000128+0000000590+000002843.56+000000148.39 2010-09-27201009
08000002728+0000008947+008282517.52+004450313.95 2010-09-27201009
12000000917+0000001974+000555783.18+000819727.45 2010-09-27201009
14000000007+0000000010+000004773.00+000001026.80 2010-09-27201009
15000000298+0000016858+000334163.89+000478644.01 2010-09-27201009
16000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
18000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
27000000078+0000000196+000009205.21+000003151.24 2010-09-27201009
21000000057+0000000219+000039995.49+000039382.41 2010-09-27201009
25000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
30000000116+0000000466+000299833.16+000005895.46 2010-09-27201009
40000002108+0000003477+000311888.91+000219808.01 2010-09-27201009
35000000010+0000000281+000033884.00+000032151.00 2010-09-27201009
28000000336+0000001258+000799387.65+000313825.85 2010-09-27201009
60000000690+0000003421+001010850.57+000564147.45 2010-09-27201009
55000000000+0000000000+000000000.00+000000000.00 2010-09-27201009
51000000615+0000002544+000010538.52+000000597.82 2010-09-27201009
52000000436+0000002738+000010654.19+000000673.83 2010-09-27201009
53000000084+0000000455+000004983.83+000000323.46 2010-09-27201009
09000002856+0000074490+001429055.86+002321155.60 2010-09-27201009
10000000332+0000001322+000006250.50+000000328.37 2010-09-27201009
11000000041+0000000176+000000968.00+000000112.64 2010-09-27201009
45000000010+0000000033+000011481.00+000008827.54 2010-09-27201009

Expected result :

Code:
000011481.00

Result coming as :
Code:
001449750.71
000188697.81
000212138.84
000126543.77
000059183.61
000004773.04
000002843.56
008282517.52
000555783.18
000004773.00
000334163.89
000000000.00
000000000.00
000009205.21
000039995.49
000000000.00
000299833.16
000311888.91
000033884.00
000799387.65
001010850.57
000000000.00
000010538.52
000010654.19
000004983.83
001429055.86
000006250.50
000000968.00
000011481.00

I am trying to fetch the corresponding column, where the first two field matches with 45. But, every time the cut is fetching the whole column.
Please help me on this .


Moderator's Comments:
Mod Comment Please use code tags and indent your code


Code:
#!/bin/ksh
for each_rec in `cat <filename>`
do
count=`echo ${each_rec}|cut -c -2 | grep "45"`
echo ${count}
if [[ "${count}" = "45" ]]; then
amount=`echo ${each_rec}|cut -c 24-35`
echo ${amount}
else
echo "failed"
fi
done

# 4  
Old 09-29-2010
The quick way:
Code:
grep \^"45" filename | cut -f3 -d+
000011481.00


Alternatively modifying your script to use a "while" not a "for" loop. There is never a reason to use a "for" loop with an open-ended list.
Also corrected various syntax anomalies. Note that "cut" either takes input from a pipeline or a file (in this case we need to use "echo" to get the record onto a pipeline).
Code:
cat filename | while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
       amount=`echo "${each_rec}" | cut -c24-35`
       echo "${amount}"
  fi
done
000011481.00

# 5  
Old 09-29-2010
Code:
awk -F + '/^45/ {print $3}' infile

000011481.00



---------- Post updated at 10:25 AM ---------- Previous update was at 10:24 AM ----------

Code:
var=45

awk -F + '/^'"$var"'/ {print $3}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with GREP + CUT - script for automatic update of STEAM GAME AR

Hello, I have a problem with the /etc/rc.d/init.d script to automatically update STEAM GAME ARK. I've converted 3 scripts into one, but something does not work correctly ... The problem is in the file latestavailableupdate.txt / line 36/39. It think the problem is with script, it wrongly... (2 Replies)
Discussion started by: kshishu
2 Replies

2. Shell Programming and Scripting

Problem in extracting data using cut/awk command

Hi Everyone, I have a very simple problem and i am stuck in that from last 8 days. I tried many attempts, googled my query but all in vain. I have a text file named "test.txt" In that suppose i have contents like: Java: 1 Object oriented programming language 2 Concepts of Abstraction... (5 Replies)
Discussion started by: Abhijeet Anand
5 Replies

3. UNIX for Dummies Questions & Answers

Problem using Grep for a loop

Dear All, I have problem with generalizing my code and I can't see where the problem is. I have a main.txt file that has all the information and then I have 4 folders (headoffice, branch, management, office) that each have a 2 of files keep.txt and throw.txt and each of them have few lines... (3 Replies)
Discussion started by: A-V
3 Replies

4. UNIX for Dummies Questions & Answers

Problem with multiple grep in bash loop

Hello, I am trying to create a matrix of 0's and 1's depending on whether a gene and sample name are found in the same line in a file called results.txt. An example of the results.txt file is (tab-delimited): Sample1 Gene1 ## Gene2 ## Sample2 Gene2 ## Gene 4 ## Sample3 Gene3 ... (2 Replies)
Discussion started by: InfoSeeker2
2 Replies

5. Shell Programming and Scripting

Using a loop with cut

I don't know if I described this right, but I am new to scripting and this is giving me a little bit of trouble, but I will explain what I am trying to do. Each time this is run, I want it to grab and save ls -l /home to data.txt. ls -l /home > data.txt Now the part I am getting confused... (4 Replies)
Discussion started by: ninjafish
4 Replies

6. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

7. UNIX for Dummies Questions & Answers

Cut not working in a loop

I have a function "MyPrint" that runs great on a file (BaseData.txt) that has one line of data. If i add rows to the text file it's reading the tFile variable becomes a list of every field 2 in the file. To correct this, i tried to call the function from a loop where i read one line at a time and... (4 Replies)
Discussion started by: KME
4 Replies

8. UNIX for Dummies Questions & Answers

grep and cut problem

Hello folks, I have to (e)grep out a certain pattern e.g. <TAG1> from a huge log file which does not have any space as such. The thing is that once I have 'grep'ed out the <TAG1> from the file I need to extract the content within the tags, i.e, <TAG1>Data_To_Be_Extracted</TAG1> The underlined... (9 Replies)
Discussion started by: Rajat
9 Replies

9. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies

10. UNIX for Advanced & Expert Users

For loop problem extracting data

I have a problem with my loops. I have a file called users.dat, it has all the users in it. Then I extracted a list of users sending out number of mails with date from Netscape logs. The extracted list (mailuse.dat) has 3 fields: username, number of mails, date. (One user can show up several... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question