- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script or command(awk, sed, head or tail...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 07:20 AM
03-14-2007 07:20 AM
There is a file which contains 9 lines within
range and keep appending with too many records. Please see the
example at the end:
How can I print from that file to tail with in
range, I don't want to break the record when I print. When I tail -n
100, as that file will be growing, I don't want to get like the
following:
Please help me.
Example of the file:(It will be huge, I just gave an instance)
Please assist me....How can I setup the begin and end rules for the above.
awk '/
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 07:42 AM
03-14-2007 07:42 AM
Re: Shell script or command(awk, sed, head or tail)
tail -11 file_name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 07:51 AM
03-14-2007 07:51 AM
Re: Shell script or command(awk, sed, head or tail)
But if I do tail -number filename, I will get:
Since the file will be growing when I tail it might end without
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 07:54 AM
03-14-2007 07:54 AM
Re: Shell script or command(awk, sed, head or tail)
# tail -n20 file | perl -ne 'print if ?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 08:05 AM
03-14-2007 08:05 AM
SolutionSo do you want a function which prints only the last whole record block?
This awk program will do that:
(r==1) {rec[tmp,i++]=$0}
/^
/^<\/record>/ {good=tmp; recs=i-1; tmp=1-tmp}
END {for (i=0;i
Line 1: If in a record block, stash away lines in tmp array portion of multidimensional record array
Line 2: If start of record block, then remember in a block, and start in begin of temp array
Line 3: If end of record block, promote complete tmp array to 'good' array, flag end, and start new tmp.
Line 4: At END, print last rec array which has last good whole block.
If this is nto exactly what you desire, maybe it help define your exact needs.
For example instead of just two 'big' values you have a circular list of N+1 and print the last N whole blocks.
For very large data volumes the most efficient solution would be to just remember (C, Perl) each
That way you could also JUMP to where a prior run left of.
hth,
Hein van den Heuvel
HvdH Performance Consulting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 08:05 AM
03-14-2007 08:05 AM
Re: Shell script or command(awk, sed, head or tail)
But it is giving only last record....even if I do tail -n60...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 08:10 AM
03-14-2007 08:10 AM
Re: Shell script or command(awk, sed, head or tail)
> James, But it is giving only last record....even if I do tail -n60...
I understood you to say that you *only* wanted the *last block* that was bounded by
That is what:
# tail -n13 file | perl -ne 'print if ?
...was designed to do, using your sample input.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 08:21 AM
03-14-2007 08:21 AM
Re: Shell script or command(awk, sed, head or tail)
awk '/
awk needs a way to find the search string delimiters so you escape the special characters between the / /. Now finding the last complete section between
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2007 08:23 AM
03-14-2007 08:23 AM
Re: Shell script or command(awk, sed, head or tail)
Ok, if you are feeding it with tail,
and if
- start remembering all records as soon as
- remember the last end seen:
tail x | awk '/^
(r) {rec[i++]=$0}
/^<\/record>/ {end=i}
END {for (i=0;i
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2007 12:01 AM
03-15-2007 12:01 AM
Re: Shell script or command(awk, sed, head or tail)
No no no...I DID NOT say I want only last block..... When I tail I WANT the whole block to be displayed. If the tail doesn't get the whole block at the end then I should be able to trim the last one....The result I should get is from block to block(I mean record to record)....
James! With your command I get the last record, but I dynamically I should be able to display the multiple records, like the following:
<....constantly 9 lines will be there>
<....constantly 9 lines will be there>
<....constantly 9 lines will be there>
<....constantly 9 lines will be there>
BUT I DO NOT WANT TO DISPLAY:
<....constantly 9 lines will be there>
<....constantly 9 lines will be there>
<....constantly 9 lines will be there>
<....constantly 9 lines will be there>
<....only 6 lines displayed(any number other than 9 lines>
I hope you all understood what I am trying to explain....
Thank you for all your time in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2007 12:21 AM
03-15-2007 12:21 AM
Re: Shell script or command(awk, sed, head or tail)
how about:
sed -n "1,`grep -n -e'' a.lis| tail -1 | cut -d':' -f1`p" a.lis
print line 1 to (find all /records, then get the line number of the last one)
a.lis is your datafile
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 shows how to reward any useful answers given to your questions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2007 01:21 AM
03-15-2007 01:21 AM
Re: Shell script or command(awk, sed, head or tail)
Thank you for your time...It is working, but how can I restrict the tail number like, tail -n 100 or 250 or etc.....
I don't want to get the whole file.....i should be able to get the number of the records, as I want....
like #tail -n 80 filename
or
# tail -n 120 filename
Your command is working fine to get the records from block to block, but how can I get the latest 50 records or 100 records etc....Do you know what I am saying....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2007 02:05 AM
03-15-2007 02:05 AM
Re: Shell script or command(awk, sed, head or tail)
tail -n 120 filename | awk '/^
(r) {rec[i++]=$0}
/^<\/record>/ {end=i}
END {for (i=0;i
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2007 02:15 AM
03-15-2007 02:15 AM
Re: Shell script or command(awk, sed, head or tail)
I like scripting, but man! How can I learn this complicated scripting....;)
Thanks again so much for you and everyone who tried to assist me...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 03:39 AM
03-19-2007 03:39 AM
Re: Shell script or command(awk, sed, head or tail)
a) "block" is bounded by
b) only complete blocks (ignore any incomplete trailing block)
c) X is variable
but I could be wrong....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 06:19 AM
03-19-2007 06:19 AM
Re: Shell script or command(awk, sed, head or tail)
I think OldSchool guessed right - at least would be a flexible solution, which seems interesting. So try:
awk -v sta='
cat last_n_blocks.awk
$1 ~ sta {found=-1;i=0;bl++}
found<0 {rectmp[++i]=$0}
$1 ~ fin {found=1; for(j=1;j<=i;j++) {rec[bl%num,j]=rectmp[j]}; rn[bl%num]=i}
END {if(found<0) bl--;for(k=(bl-num+1);k<=bl;k++) {for(j=1;j<=rn[bl%num];j++) print rec[k%num,j]}}
You see, you can set the start / end pattern as well as the number of output blocks.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 12:14 AM
03-22-2007 12:14 AM
Re: Shell script or command(awk, sed, head or tail)
Thanks for the reply...sorry I was in training so couldn't check this....
Anyways, how do I execute this? My file name is xmlfile. When I tried to execute the following it errored out as last_n_blocks.awk couldn't find. Can you bear with me and explain please....
awk -v sta='
cat last_n_blocks.awk
$1 ~ sta {found=-1;i=0;bl++}
found<0 {rectmp[++i]=$0}
$1 ~ fin {found=1; for(j=1;j<=i;j++) {rec[bl%num,j]=rectmp[j]}; rn[bl%num]=i}
END {if(found<0) bl--;for(k=(bl-num+1);k<=bl;k++) {for(j=1;j<=rn[bl%num];j++) print rec[k%num,j]}}
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 12:22 AM
03-22-2007 12:22 AM
Re: Shell script or command(awk, sed, head or tail)
have you got your last_n_blocks.awk in the correct directory ? Any typing mistakes in the filenames ?
It works for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 12:27 AM
03-22-2007 12:27 AM
Re: Shell script or command(awk, sed, head or tail)
Do you want to me to create last_n_blocks file and insert the following lines as follows:
$1 ~ sta {found=-1;i=0;bl++}
found<0 {rectmp[++i]=$0}
$1 ~ fin {found=1; for(j=1;j<=i;j++) {rec[bl%num,j]=rectmp[j]}; rn[bl%num]=i}
END {if(found<0) bl--;for(k=(bl-num+1);k<=bl;k++) {for(j=1;j<=rn[bl%num];j++) print rec[k%num,j]}}
And then do I need to execute the following at the command prompt:
# awk -v sta='
$$$$ xmlfile is the output of my records file which I would like to trim.....
Thank you...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 12:38 AM
03-22-2007 12:38 AM
Re: Shell script or command(awk, sed, head or tail)
Create the last_n_blocks.awk file and insert the script as is.
Then run the awk line (Please note your latest post had a typingmistake in the input filename (xmfile rather than xmlfile))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 12:39 AM
03-22-2007 12:39 AM
Re: Shell script or command(awk, sed, head or tail)
you nearly have got it:
I want you to create a file
last_n_blocks.awk
with the content you saw from the cat-command and then execute the statement
awk -v sta='
where xmlfile contains the data to be trimmed.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 02:20 AM
03-22-2007 02:20 AM
Re: Shell script or command(awk, sed, head or tail)
Thank you so much...it is working perfectly.... This is what I am looking for...
I am happy now......
But the thing is I want to learn and will be more happy if I do on my own...
I really appreciate for your assistance and time.
Thanks again...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 02:34 AM
03-22-2007 02:34 AM
Re: Shell script or command(awk, sed, head or tail)
>> But the thing is I want to learn and will be more happy if I do on my own...
Good attitude!
be sure to read the man pages, or buy a book.
Keep your eyes open in this
forum for other (simple) awk question.
Realize that AWK is just a simple, limited, start.
Move over to PERL as soon as possible for just a little more work, but a lot more possibilities.
Finally, be sure to check my earlier, partial, solution where I gave a detailed stpe by step explanation applicable to your problem/context.
Good luck in the learning!
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 02:45 AM
03-22-2007 02:45 AM
Re: Shell script or command(awk, sed, head or tail)
Anyways, I should build linux at home so that I can play around....
Thanks anyways for your suggestion and support...
Have a good one!
Thank you