Script needed to extract few lines from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script needed to extract few lines from file
# 1  
Old 02-12-2013
Script needed to extract few lines from file

Hello,
I need a utility script or command that will extract the following lines from a file based on a 'word' contain in a line. For example my file contains lot of lines.
So if i pass 1800182 to the script/command it should return everything between 1st RequestNetRates tag before it and 1st RequestNetRates tag after it ...thanks in advance

Code:
<RequestNetRates xmlns="http://xmlns.fedex.com/RateVisibility">
    <VersionNumber>1.3.3</VersionNumber>
    <TransactionDate>2013-02-12T04:14:02Z</TransactionDate>
    <RateDate>2013-02-12</RateDate>
    <RequestKey>INTLRS_1800182_en_US_KG_IPDE</RequestKey>
    <Express>
        <RequestParameters>
            <AccountNumber>610013406</AccountNumber>
            <RateCurrencyCode>EUR</RateCurrencyCode>
            <RegionLocatorPageFlag>Y</RegionLocatorPageFlag>
            <RateWeightUnitOfMeasureCode>KG</RateWeightUnitOfMeasureCode>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>01I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>37I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>86I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>18I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>25I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>15I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>17I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>61I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>84I</ServiceDescriptionCode>
            </ServiceDescriptionData>
            <ServiceDescriptionData>
                <ServiceDescriptionCode>70I</ServiceDescriptionCode>
            </ServiceDescriptionData>
        </RequestParameters>
    </Express>
</RequestNetRates>

# 2  
Old 02-12-2013
Provide a sample of what the output should look like...
# 3  
Old 02-12-2013
let's say your file is data.xml

-
Code:
totalline=`cat data.xml |wc -l`
#get line num containing pased data
ln=`cat -n|grep -P '^.*$1*$'|head -1|grep -oP '^[0-9]+'
#firstnode according to your given data is 4 lines above this line so
ln=`echo $ln -4 |bc`
#now we need 2nd appearance of RequestNetRates from here (including this line)
 
temp=`echo $totalline-$ln|bc`
lastline=`cat -n data.xml |tail -$temp |grep RequestNetRates|head -2 | tail -1 | grep -wP '^[0-9]*'`
 
#so we get firstnode RequestNetRates at ln and 2nd node at lastline
 
# now to get content
lastline=`echo $lastline-$ln|bc`
tail -temp data.xml|head -$lastline >filterdata.xml



I think this code will work. revert in case of explanation
# 4  
Old 02-12-2013
thanks it worked!
# 5  
Old 02-12-2013
Code:
awk -F'[<>]' -v D="1800182" ' /RequestKey/ {
                v = $3;
                sub(/[a-zA-Z0-9]*_/,X,v);
                sub(/_.*/,X,v);
                if(v == D) {
                        f = 1;
                        print T;
                }
        } /<\/RequestNetRates>/ && f {
                print $0;
                f = 0;
                T = "";
        } f == 0 {
                T = T RS $0 ;
        } f == 1 {
                print $0;
} ' data.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract lines from a file

Hi all; Here is my file which contains a list of files (recent versions of files are in red). This file is dynamic, files versions can change at any time (versions can increment) filename ------------------------------------------------------- ... (8 Replies)
Discussion started by: chercheur111
8 Replies

2. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

3. Shell Programming and Scripting

How to extract certain lines from a file?

Hi guys I have a several thousands line file in the following format: n817 -------------------------------------------------- n842 -------------------------------------------------- n877 -------------------------------------------------- n513 /bb/data/rmt2db.lrl:JBSKDB 31915 75... (4 Replies)
Discussion started by: aoussenko
4 Replies

4. Shell Programming and Scripting

Extract particular lines from a file

Hi all, I have a file with many records with information as given below ID A16L2_HUMAN Reviewed; 619 AA. AC Q8NAA4; A5PL30; B2RPK5; Q658V4; Q6PID3; Q8NBG0; DT 20-MAY-2008, integrated into UniProtKB/Swiss-Prot. DT 20-MAY-2008, sequence version 2. DT ... (1 Reply)
Discussion started by: kaav06
1 Replies

5. Shell Programming and Scripting

Help needed to extract distinct logs from a Log File

Hi, I urgently need some help how to extract distinct entries from a Log file. The Log File may have same error occuring many times so how do i count the occurance of an error in file and also extract out distinct errors in a file. Eg:- I have a file name A.log it contains entries as below:-... (5 Replies)
Discussion started by: roro
5 Replies

6. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

7. Shell Programming and Scripting

Script to extract certain lines

Hi I have a text file with the following information: # List 1 (first header) test 1 test 2 test 3 ... # Trials (second header) round 1 run 5 ... and so on I want to create a script, which based on some criterias with return only the list of lines between the header. I... (9 Replies)
Discussion started by: nimo
9 Replies

8. Shell Programming and Scripting

how to write shell script to extract lines we want

hi i have a file which is very large . it contains lines in the format below: seed url, html url .... ... seed url, html url i have sort it already. 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ÕżªÁúרÀ¸£ºÊÀ½ç±*24ÄêµÄ»ØÒä ÆÚÅÎÑÇÖÞδÀ´ÍŽá_2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ¹úÃ×Óë±´ÄáÌØ˹´ï³ÉÐ*Òé... (6 Replies)
Discussion started by: rainboisterous
6 Replies

9. Shell Programming and Scripting

Script that extract some lines from a file lookin into another

I want a ksh script that parse two files (text files, actually my original files are .xls) - input data: - one file file1 containig lines separated by spaces (or other delimiter) - 2nd one file2 contain only one numerical value in a line (for simplicity but it might of the same form as the 1st)... (7 Replies)
Discussion started by: heartwork
7 Replies

10. UNIX for Dummies Questions & Answers

logic needed to extract lines

Hi All, Are there any logic to extract the only lines between first two *TM* (which is marked in blue)? VOL1HST99 0 HDR1A999999S 999999HST99 HDR2F001200012001 UHL1 0729609000001 000000 *TM*^^^^^^^^^^^^^^^^^^^^^^ 01012610252171017301805000... (2 Replies)
Discussion started by: ganapati
2 Replies
Login or Register to Ask a Question