Fetch data from a particular location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch data from a particular location
# 1  
Old 07-11-2012
Fetch data from a particular location

I want to fetch value from a particular location from a file but in each line in the file it appears at a different position so i tried using variable with cut command but it is not working properly. The code i have written is


Code:
#!/bin/sh
cat Sri1.log |
while read d2
do
   grep -w 'RemittanceId' | awk -v find='RemittanceId' '{loc=index($0,find)}' 
   locations=`expr $loc + 13`
   locatione=`expr $location + 15`
   cut -c $locations,$locatione >> ab.txt
done

I am storing the start and end values in the variables but it's not working fine. It doesn't give any output


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 03:08 AM.. Reason: code tags
# 2  
Old 07-11-2012
there is no input for grep command

you didnt use d2 value anywhere in the script

Code:
echo $d2 | grep -w 'RemittanceId' | awk -v find='RemittanceId' '{loc=index($0,find)}'

Also, instead of using cat, you can use it like this

Code:
 
while read d2
do
...
...
done < Sri1.log

---------- Post updated at 11:42 AM ---------- Previous update was at 11:41 AM ----------

also, you didnt print the loc value in the awk

and what is $loc and $locations ?
# 3  
Old 07-11-2012
Thanku for the reply
I tried doing that.It works fine but not givingme the desired output since

loc returns value 0

but when a try to write the output of awk in a file it gives the all the locations at which value is present.

Hiw can a fetch the value of loc the moment it encounters taht line and moving on to next line.

I tried defining the variable locations and locatione inside the action part of awk but it didn't worked

echo $d2|grep -w 'RemittanceId' | awk -v find='RemittanceId' '{loc=index($0,find), locations=`expr $loc + 13` , locatione=`expr $location + 15`}'
# 4  
Old 07-11-2012
can you provide the sample log file contents and the expected output
# 5  
Old 07-11-2012
I can't share complete file but below is the line from the file :-

2012-05-09 10:29:30.041.... | SYNC | 00052 | DEBUG | Payment | ....Exchange | Exchange processing completed.....| B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003 | ....... | {previous-component=Validation, Split, exchange-properties={....., RemittanceId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=....., }

I want to fetch this RemittanceId from the file..
There are many occurrences of this Id in the whole file and at different locations

The desired output is
IUxBNzKlZfghcMxf101209090000024_1
# 6  
Old 07-11-2012
Code:
$ nawk -F"[=,]" '{for(i=1;i<=NF;i++)if($i~/RemittanceId/)print $(i+1)}' input.log
IUxBNzKlZfghcMxf101209090000024_1

if you dont have nawk, then try with awk or gwak

---------- Post updated at 12:48 PM ---------- Previous update was at 12:45 PM ----------

Code:
 
$ nawk -F, '/RemittanceId/{split($4,a,"=");print a[2]}' input.log
IUxBNzKlZfghcMxf101209090000024_1

# 7  
Old 07-11-2012
Thanx a lot it is working fine.. I am greatful
Can you please explain me this command since I am new to Unix and don't have much understanding how it is working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch data from file

Hi, I am new to scripting. I have a log file and need to fetch specific logs and copy to another file. A copy of the log is like this: =============================================================== = JOB : server123#jobs1.jobstream1 = USER : andyc = Tue 08/01/17... (3 Replies)
Discussion started by: Prngp
3 Replies

2. UNIX for Beginners Questions & Answers

How to fetch specific data from a file.?

Hi , I have a file which contains 2 days logs(here it is 24 and 25) I want to list data only for date 25 fron the file. please suggest me how should i get this. file content mentioned below 17-05-24 Name Succ Fail 00:00:29 ... (5 Replies)
Discussion started by: scriptor
5 Replies

3. Shell Programming and Scripting

Need to fetch only selected data in CSV

Hi Team, I m getting my script commands output like given below GETA-TILL-INF; U-UU-YRYT-NOD-6002 2015-05-14 THU 19:44:10 C2221 RETRIEVE TILL INFORMATION : COMPLD ---------------------------------------------------------------------- CONNECT_CARD_ID ... (9 Replies)
Discussion started by: Ganesh Mankar
9 Replies

4. Shell Programming and Scripting

Help Need to fetch the required data

Hi Guys, Am in need of your help one more time on my real data. I have a file which contains more than thousand lines of data Live data shown for 4 iterations. We have more than thousand lines of data:- -------------------------------------------------------------------------- ... (4 Replies)
Discussion started by: rocky2013
4 Replies

5. Shell Programming and Scripting

Fetch Data from File using UNIX or Perl

Hello, How All are Doing today. I have a issue, I have a file which contains the data as follow <ENVELOPE><ENVELOPE_ID>TEST</ENVELOPE_ID><ENVELOPE_EXTERNAL_ID></ENVELOPE_EXTERNAL_ID><ENVELOPE_VERSION>2</ENVELOPE_VERSION><SIResourceDefaultVersion>true</SIResourceDefaultVersion><TYPE>GS... (1 Reply)
Discussion started by: adisky123
1 Replies

6. UNIX for Dummies Questions & Answers

how to fetch data in unix

Hi All, I have a file with the below data as shown. A|2|20120430 B|EMP|NAME|DEPT C|12|SARC|01 C|23||ASDD|02 D|END OF FILE I want to fetch only the records that contains C|, what is unix command to fetch this data. Thanks (5 Replies)
Discussion started by: halpavan2
5 Replies

7. Shell Programming and Scripting

Fetch data between two dates from a file

Hi All, I m new to this forum & UNix too. currently i have a requirement which can fetch data from a logfile between two dates or timestamp. for example: 1. data from 2012 Jun to 2012 Jul 2. data from 2012 Jun to 2012 Jul 07 3. data from 2012 Jun 16 10:20 to 2012 Jul 03 10:10 Please... (7 Replies)
Discussion started by: KDMishra
7 Replies

8. Shell Programming and Scripting

fetch data between two timestamp using script

Hi Guys, I have the data in below format. 25 Dec 2011 03:00:01 : aaaaaaaaaaaaaaa 25 Dec 2011 04:23:23 : bbbbbbbbbbbbbbb 25 Dec 2011 16:12:45 : ccccccccccccccc 26 Dec 2011 04:45:34 : ddddddddddddddd 26 Dec 2011 17:01:22 : eeeeeeeeeeeeeee 27 Dec 2011 12:33:45 : ffffffffffffffffffffffff 28... (13 Replies)
Discussion started by: jaituteja
13 Replies

9. Shell Programming and Scripting

Script to fetch data from HTML

Hi All, There is a link from were I usually search somthing and fetch the data from. Is there any way to automate it through a script if I mention search criteria in a note pad. I mean the script to search the content on the notepad and resutls should be placed into another file. ... (2 Replies)
Discussion started by: indradev
2 Replies

10. Shell Programming and Scripting

Fetch selected data from webpage

Hi All, Can anybody tell me the command used for extracting some selected lines from a web-page. I guess we'll have to do this using wget or Curl to achieve this.... If anbody has any idea abt it, kindly post your reply ASAP. Thanks. (1 Reply)
Discussion started by: sunnydynamic15
1 Replies
Login or Register to Ask a Question