- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Challenging Script question
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
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
04-21-2006 03:33 AM
04-21-2006 03:33 AM
Re: Challenging Script question
First you are going to have to calculate the "middle" of a file.
Say the file has 8 lines -- the middle would be after the 4th line (8 / 2 = 4)
Now the problem comes if the file has 9 lines (or any other odd number). What do you consider the middle? Since 9 / 2 = 4.5 do you truncate and insert after the 4th line or round up and insert after the 5th line?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 03:35 AM
04-21-2006 03:35 AM
Re: Challenging Script question
try this:
#!/bin/ksh
...
# $tempfile contains file to be injected
# $NF contains original file
typeset -i COUNT=`wc -l <$NF`
awk -v c=$((COUNT/2)) -v inj=$tempfile '{print}
NR==c {while ((getline
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 03:40 AM
04-21-2006 03:40 AM
Re: Challenging Script question
posting missed a ')' in close(inj).
In a view of Patricks comment:
For a odd number N of lines, the text is inserted at (N-1)/2 .
Inserting at (N+1)/2 is left to the reader as an exercise...
#!/bin/ksh
...
# $tempfile contains file to be injected
# $NF contains original file
typeset -i COUNT=`wc -l <$NF`
awk -v c=$((COUNT/2)) -v inj=$tempfile '{print}
NR==c {while ((getline
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 04:22 AM
04-21-2006 04:22 AM
Re: Challenging Script question
Im getting an error message when I try to run it.
It says:
$NF: ambiguous redirect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 04:32 AM
04-21-2006 04:32 AM
Re: Challenging Script question
To insert in the middle of the file, this variation trades memory for I/O. THat is, the input file is read *once* into memory instead of twice, the first time to obtain the number of lines it contains.
The input file is updated "in-place", too:
cat .inject.pl
#!/usr/bin/perl
use strict;
my $i;
my $oldfile = pop;
my $newfile = "$oldfile.new";
open(FIN ,"<",$oldfile) or die "Can't open '$oldfile': $!\n";
open(FOUT,">",$newfile) or die "Can't open '$newfile': $!\n";
my @lines =
for ($i=0; $i < ($#lines/2); $i++) {print FOUT $lines[$i]};
print FOUT "@ARGV\n";
for ($i=$i; $i <= $#lines+1; $i++) {print FOUT $lines[$i]};
close FOUT;
rename($newfile, $oldfile);
1;
# ./inject.pl This is in the Middle! FileName
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 04:39 AM
04-21-2006 04:39 AM
Re: Challenging Script question
>> COUNT=`expr $COUNT \ 2`
I presume this is the expression you are using in your script and it is not simply a typo since the division operator is / not \. So change the above to:
COUNT=`expr $COUNT / 2`
Consider adding 1 to the line count before dividing since that's closer to the middle for odd-numbered lines which have no middle line i.e. 11/2=5.5. It will have no effect on even numbered lines since integer division truncates
COUNT=`cat $NF | wc -l`
let COUNT=COUNT+1
COUNT=`expr $COUNT / 2`
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 06:30 AM
04-21-2006 06:30 AM
Re: Challenging Script question
#!/bin/sh
echo $* | awk '{
"wc -l <"$NF | getline cnt
mid=int((cnt+1)/2)
system("split -l "mid" "$NF)
for(i=1;i
system("cat xaa tmpfile xab >"$NF)
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 07:10 AM
04-21-2006 07:10 AM
Re: Challenging Script question
"wc -l <"$NF | getline cnt
check the file for number of lines then pipe it to getline and then store it in cnt.
Have I got it right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 07:14 AM
04-21-2006 07:14 AM
Re: Challenging Script question
:)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 07:19 AM
04-21-2006 07:19 AM
Re: Challenging Script question
Im assuming that at the end of the script it would be appropriate to remove xaa and xab. Would it make a big difference if they were not removed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 07:21 AM
04-21-2006 07:21 AM
Re: Challenging Script question
>"wc -l <"$NF | getline cnt
>check the file for number of lines then pipe it to getline and then store it in cnt.
>Have I got it right?
Yes that is exactly right. Remember that NF is an awk variable and is not available for processing outside of the awk construct.
NF==number of fields
$NF==file1
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 07:29 AM
04-21-2006 07:29 AM
Re: Challenging Script question
>Im assuming that at the end of the script it would be appropriate to remove >xaa and xab. Would it make a big difference if they were not removed?
xaa and xab are temporay files created for the purpose of inserting a line in the middle of file1 and they can be removed at the end of the script. The "rm xaa xab" does not need to be inside the awk construct though i.e.
#!/bin/sh
echo $* | awk '{
"wc -l <"$NF | getline cnt
mid=int((cnt+1)/2)
system("split -l "mid" "$NF)
for(i=1;i
system("cat xaa tmpfile xab >"$NF)
}'
rm xaa xab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 07:34 AM
04-21-2006 07:34 AM
Re: Challenging Script question
- « Previous
-
- 1
- 2
- Next »