- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- 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-18-2006 04:20 AM
04-18-2006 04:20 AM
Line1
Line2
Line3
Line4
Line5
Im trying to write a script that takes two arguments. The first being a line of text and the second the name of the file above. How do I insert the first argument(line of text) into the first line of the file named above.
For example:
./scriptTest Hello this is a test File1
Would append File1 as such:
Hello this is a test
Line1
Line2
Line3
Line4
Line5
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:25 AM
04-18-2006 04:25 AM
Re: Challenging Script question
filename=$2
(echo $header ;cat $filename) > /tmp/mytmpfile
mv /tmp/mytmpfile $filename
one of the ways how to do it..
HTH
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:26 AM
04-18-2006 04:26 AM
Re: Challenging Script question
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:26 AM
04-18-2006 04:26 AM
Re: Challenging Script question
echo $1 > /tmp/file
cat $2 >> /tmp/file
cp /tmp/file $2
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:32 AM
04-18-2006 04:32 AM
Re: Challenging Script question
Using the shell only:
cat ./insertit
#!/usr/bin/sh
echo "${1}" >> "${2}.new"
cat ${2} >> "${2}.new"
mv "${2}.new" "${2}"
# ./insert it ok yourfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:47 AM
04-18-2006 04:47 AM
Re: Challenging Script question
So far I have managed the following.
sed -e '1i\' -e "$*" > File1
But this appends the whole line including the second argument.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:00 AM
04-18-2006 05:00 AM
Re: Challenging Script question
f=$1
t=/tmp/hold$$
shift
echo $* | cat - $f >$t
mv $t $f
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:01 AM
04-18-2006 05:01 AM
Re: Challenging Script question
echo hello this is a test file1 | awk '{
for(i=1;i
}'
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:03 AM
04-18-2006 05:03 AM
Re: Challenging Script question
The line of text has to come first then the file name.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:10 AM
04-18-2006 05:10 AM
Re: Challenging Script question
Im not sure that will work. How does the script know to echo the line of text: Hello this is a test, unless you manually type it in the script.
It could be any line of text.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:10 AM
04-18-2006 05:10 AM
Re: Challenging Script question
=================myshellscript=================
#!/bin/sh
echo $* | awk '{
for(i=1;i
}'
=============================================
Give your script a name "myshellscript" make it executable "chmod" and execute it at the command line as...
# ./myshellscript
hope it helps!
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:12 AM
04-18-2006 05:12 AM
Re: Challenging Script question
given file1
Line1
Line2
Line3
Line4
Line5
./script Hello world file1
script:
touch .mytempx
rm .mytemp?*
echo $@ | awk '{print "cp ", $NF, " .mytempfile2" }' > .mytempx
echo $@ | awk '{print "cat ", " .mytempfile2 >> .mytempfile3"; print " mv .mytempfile3 ",$NF }' > .mytempx2
chmod +x .mytempx .mytempx2
./.mytempx
echo $@ | awk '{$1==$NF="";print $0}' > .mytempfile3
./.mytempx2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:20 AM
04-18-2006 05:20 AM
Re: Challenging Script question
# myshellscript Hello this is a test file1
...passing command line arguments to the script instead of running it w/o any.
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:29 AM
04-18-2006 05:29 AM
Re: Challenging Script question
Here's yet another way to offer the user the ability to specify the name of the file *last* and the string to insert *first* without the need to quote anything:
# cat .inject.pl
#!/usr/bin/perl
my $file = pop;
open(FH,"<",$file) or die "Can't open $file\n";
print "@ARGV\n";
print while (
1;
For example:
# ./inject.pl Hello this is a test FileName
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:29 AM
04-18-2006 05:29 AM
Re: Challenging Script question
set -A a $*
(( n = ${#a[*]} - 1 ))
f=${a[$n]}
a[$n]=""
t=/tmp/hold$$
echo ${a[*]} | cat - $f >$t
mv $t $f
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 05:59 AM
04-18-2006 05:59 AM
Re: Challenging Script question
Just tried your code but it seems to delete everything in the file and then write the line of text to the file.
Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 06:27 AM
04-18-2006 06:27 AM
Re: Challenging Script question
If you prefer not to preserve your original file, but use the Perl script I suggested, then use this version:
# cat .inject.pl
#!/usr/bin/perl
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";
print FOUT "@ARGV\n";
print FOUT while (
close FOUT;
close FIN;
rename($newfile, $oldfile);
1;
For example:
# ./inject.pl Hello this is a test FileName
In this variation, the modified file ("FileName") is updated, "in-place".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 06:35 AM
04-18-2006 06:35 AM
Solution>Any ideas?
Here's the updated version of "myshellscript" so that file1 doesn't get clobbered and the text gets written above the first line in file1:
=============================================
#!/bin/sh -x
echo $@ | awk '{
system("mv "$NF" tmpfile1")
for(i=1;i
system("cat tmpfile1 >>"$NF)
}'
=============================================
hope it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 06:47 AM
04-18-2006 06:47 AM
Re: Challenging Script question
It worked perfectly. Thank you.
Any chance that you could explain the code as it is a bit advanced for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 06:50 AM
04-18-2006 06:50 AM
Re: Challenging Script question
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 06:55 AM
04-18-2006 06:55 AM
Re: Challenging Script question
How much of the code would need to be modified if I wanted to insert the line in the middle of the file?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 06:58 AM
04-18-2006 06:58 AM
Re: Challenging Script question
#!/bin/sh
echo $* | awk '{
system("mv "$NF" tmpfile1")
for(i=1;i
system("cat tmpfile1 >>"$NF)
}'
The command line arguments are echoed and piped to the awk where they are split into different fields, NF being the last one and in this case it's the filename.
The system command renames (mv) file1 to tmpfile1; printf writes all the fields except the last one to file1; and finally the contents of tmpfile1 are appended (cat) to file1.
hope it makes sense
:)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 07:13 AM
04-18-2006 07:13 AM
Re: Challenging Script question
How much of the code would need to be modified if I wanted to insert the line in the middle of the file?
If you knew where exactly you need to add the "Hello this is a test" for example say it was before line4; then split file1 into two files (say f1 and f2); run the awkscript; and concatenate f1 file1 f2 together to produce the desired result as folows:
#!/bin/sh
echo $* | awk '{
system("split -l 3 "$NF)
for(i=1;i
system("cat xaa tmpfile1 xab >"$NF)
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 07:14 AM
04-18-2006 07:14 AM
Re: Challenging Script question
>How much of the code would need to be modified if I wanted to insert the line
>in the middle of the file?
If you knew where exactly you need to add the "Hello this is a test" for example say it was before line4; then split file1 into two files (say f1 and f2); run the awkscript; and concatenate f1 file1 f2 together to produce the desired result as folows:
#!/bin/sh
echo $* | awk '{
system("split -l 3 "$NF)
for(i=1;i
system("cat xaa tmpfile1 xab >"$NF)
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2006 03:17 AM
04-21-2006 03:17 AM
Re: Challenging Script question
How can you insert a line exactly in the middle of the file.
I have tried the following but it doesnt work. Any ideas?
#!/bin/sh
COUNT=`cat $NF | wc -l`
COUNT=`expr $COUNT \ 2`
echo $* | awk '{
system("split -l $COUNT "$NF)
for(i=1;i
system("cat xaa tmpfile1 xab >"$NF)
}'