To get the id value dynamically


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
To get the id value dynamically

Hi,

I have .html file and need to get the id value based on the latest modified date from the html file.

output : 1456.

Thanks in advance

Here is the .html file

Code:
<html>
<head><title>Index</title>
</head>
<body>
<h1> BI EI Team</h1>
<pre>ID   Last modified      Size</pre><hr/>
<pre>
<a href="1456/">1456/</a>  01-Mar-2019 15:49    108MB
<a href="4561/">4561/</a>  28-Feb-2019 11:08    121MB
</pre>
<hr/>
</body>
 </html>



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-06-2019 at 06:41 AM.. Reason: Added CODE tags.
# 2  
Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.



So - what have you tried so far?
Do you have GNU date available, or another version that allows a to-be-operated-upon-date parameter?

Last edited by RudiC; 03-06-2019 at 06:52 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Thanks Rudic,

I tried with below command but not accurate value am getting.

Code:
cat file.html |grep "<a href=" | head -2 |awk -F ">" '{print $2}

output : 1456/</a




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-06-2019 at 07:10 AM.. Reason: Added CODE tags.
# 4  
There is a closing single quote missing in your command pipeline, and the output will be the two lines with data from your input file:
Code:
1456/</a
4561/</a

Please be aware that none of the usual *nix text tools is well suited to handle *ml or similar data; any solution based on those will be crooked.

What is the essential criterion to identify the lines to extract from your html file? I used the field count (NF > 4) in this approach:
Code:
awk -F"[- :/]*" '
        {gsub (/<[^>]*>/, "")
        }
!NF     {next
        }
NF > 4  {print $4, $3, $2, $5, $6, $1
        }
' file | LC_ALL=C sort -nr -k1,1 -k2,2Mr -k3 | head -1 | cut -d" " -f6
1456

It depends on sort (GNU coreutils) to sort abbreviated month names.
This User Gave Thanks to RudiC For This Post:
# 5  
Hi ,

I achieved the result as my own. Is there any better way to do this.

Code:
cat file.html | grep "<a href=" | head -2 |awk -F ">" '{print $2}'| awk -F "/" '{print $1}'


Thx
# 6  
If you really want the id of the newest match, take RudiCs snippet. If you just assume that the first match is the newest, you can go simpler by using that:

Code:
awk 'match($0,/<a href="([0-9]+)/,res) {print res[1];exit}' data.html

If someone wants to use a parser for more robust operation, one can use this as a starting point:
Code:
xmlstarlet sel -t -v "/html/body/pre" data.html  | awk '/^\s*[0-9]+\// {print $1,$2}'


# output 

1456/ 01-Mar-2019
4561/ 28-Feb-2019

Note: I assume only gnu awk supports match(subject,pattern,array) Other variants of awk only support 2 parameter match like match(subject,pattern), which will cause a syntax error here.

Last edited by stomp; 03-06-2019 at 08:34 AM..
These 2 Users Gave Thanks to stomp For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Concatenation of different reports dynamically

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

2. Shell Programming and Scripting

How to dynamically name as function in shell?

Hi all, Does anyone know if it possible to append a parameter to a function name? Something like the following: function tnsrec_${SERVICE_NAME} { code.. } Any ideas? (6 Replies)
Discussion started by: jonnyd
6 Replies

3. Shell Programming and Scripting

Swap of fields dynamically

Dear Friends, I have file a.txt 1|2|3|4|5|6|7|8 a|b|c|d|e|f|g|h i am using the below code to swap the fields in file awk -F\| '{print $5,$1,$2,$3,$4,$6,$7,$8}' OFS=\| a.txt > output.txt output.txt 5|1|2|3|4|6|7|8 e|a|b|c|d|f|g|h The above command is working fine. I am... (4 Replies)
Discussion started by: i150371485
4 Replies

4. Shell Programming and Scripting

Dynamically choosing the interpreter

Hi, Is it possible to choose the inerpreter conditionally. For example, if whereis bash returns /usr/bin/bash then i need to choose #!/usr/bin/bash else i need to use #!/usr/bin/sh. Is it possible to achieve in a shell script? Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

5. Programming

Dynamically allocated structures in C

I have a pointer to a structure containing an integer pointer: struct members { int id; int *neigh; }; The number of members N and its neighbors M change as the code runs, so I allocate the memory dynamically: members *grid = malloc(sizeof(members)*N); for(i=0;i<N;i++)... (2 Replies)
Discussion started by: brinch
2 Replies

6. Shell Programming and Scripting

Dynamically create arrays

I have a bash challenge. I have several virtualization hosts for which I need to collect VM information. I need to dynamically create an array for each host. The contents of the array should be the list VMs running on the host. Here's the logic; I just need someone to help me with the details:... (0 Replies)
Discussion started by: EdgarTorres
0 Replies

7. Shell Programming and Scripting

set array name dynamically

Hi, I am trying to name a set of arrays dynamically named as @array_$i (The $i will mean that I obtain a set of arrays called: @array_1 @array_2 @array_3 etc. i tried various methods like @array_$i @{array_$i} etc. any ways to do this Thanks (4 Replies)
Discussion started by: esham
4 Replies

8. Shell Programming and Scripting

Dynamically locating a file

Hi, I have a requriement where in I need to install a s/w by executing the installable file through a script. The script currently contains the path of the installable file. I need to now update the script accordingly such tht it identifies the location of the installable file automatically and... (1 Reply)
Discussion started by: yoursdavinder
1 Replies

9. Shell Programming and Scripting

Spliting the file dynamically

i am creating the file , when this file reaches the size 2 GB, i need one message or fire (4 Replies)
Discussion started by: kingganesh04
4 Replies

10. Shell Programming and Scripting

getting value from field dynamically

how i do pass in field number when cutting, I tried to do this: but it says 11-1: not found (12 Replies)
Discussion started by: finalight
12 Replies

Featured Tech Videos