- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- use of VI editor in script
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
02-13-2006 10:39 PM
02-13-2006 10:39 PM
I want to write a script where I need to read one file. First line contains one name and rest of the lines contains data. I need to read that name, rename this file with that name and delete that first line from the file.
Eg. cat abc
xyz
123
12123
2q32
Now I need to rename abc to xyz and delete first line "xyz" from abc and save the file as xyz.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 10:48 PM
02-13-2006 10:48 PM
Re: use of VI editor in script
#!/bin/sh
oldfile=abc
newfile=$(sed -e '1!d' ${oldfile})
count=$(wc -l ${oldfile} | awk '{ print $1 }')
sed -e '2,${count}!d' ${oldfile} > ${newfile}
# end
exit 0
--
Muthu
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 10:57 PM
02-13-2006 10:57 PM
Re: use of VI editor in script
oldfile=abc
newfile=$(sed -e '1!d' ${oldfile})
awk '(NR != 1) { print; }' ${oldfile} > ${newfile}
--
Muthu
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 10:59 PM
02-13-2006 10:59 PM
Re: use of VI editor in script
rm -f ${oldfile}
in above two solutions.
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 11:09 PM
02-13-2006 11:09 PM
Re: use of VI editor in script
Basically, you need to get the first line "xyz". Use sed to do that. Then, move the file abc --> xyz. Delete the first line in xyz.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 11:12 PM
02-13-2006 11:12 PM
Re: use of VI editor in script
This should be useful, http://www.student.northpark.edu/pemente/sed/sed1line.txt
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 11:31 PM
02-13-2006 11:31 PM
Re: use of VI editor in script
Try this:
perl -naF -e '$old=$ARGV;if ($.==1) {open(FH,">",$F[0]);next};print FH' filename
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 11:33 PM
02-13-2006 11:33 PM
Solutionscript scr1------------------starts
#!/usr/bin/ksh
file=$1
var1=`cat $file | xargs | awk '{ print $1 }'`
sed "s/$var1//" $file | grep -v " " > file2
mv file2 $file
scprit scr1------------------ends
# chmod +x scr1
# cat abc
xyz
123
12123
2q32
Hope that helps.
Regards,
# ./scr1 abc
# cat abc
123
12123
2q321
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 11:34 PM
02-13-2006 11:34 PM
Re: use of VI editor in script
...and if you want to remove the original file, use this version:
perl -naF -e '$old=$ARGV;if ($.==1) {open(FH,">",$F[0]);next};print FH;END{unlink $old}' filename
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2006 11:40 PM
02-13-2006 11:40 PM
Re: use of VI editor in script
script should be like this:
#!/usr/bin/ksh
file=$1
var1=`cat $file | xargs | awk '{ print $1 }'`
sed "s/$var1//" $file | grep -v " " > file2
mv file2 $var1
This will create the file with the name xyz.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2006 12:09 AM
02-14-2006 12:09 AM
Re: use of VI editor in script
#!/bin/sh
oldfile=abc
newfile=$(head -1 ${oldfile})
perl -ne 'print if ($. != 1)' ${oldfile} > ${newfile}
rm -f ${oldfile}
# END
exit 0
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2006 12:41 AM
02-14-2006 12:41 AM
Re: use of VI editor in script
Simple awk solution:
awk 'END{system("rm -i " ARGV[1])} {if (NR==1) {f=$1} else {print >> f}}' x
- at end (ask to) delete the file
- if line 1 then pickup filenam in f
- any other line, print to file f
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2006 01:16 AM
02-14-2006 01:16 AM
Re: use of VI editor in script
Thanks for all help. I have used user solution and it resolved my problem.
Many thanks for your help.