Operating System - Linux
1830225 Members
1746 Online
109999 Solutions
New Discussion

use of VI editor in script

 
SOLVED
Go to solution
yogesh_4
Regular Advisor

use of VI editor in script

Hi All,
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.
12 REPLIES 12
Muthukumar_5
Honored Contributor

Re: use of VI editor in script

Use this:

#!/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



Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: use of VI editor in script

Another way as,

oldfile=abc
newfile=$(sed -e '1!d' ${oldfile})
awk '(NR != 1) { print; }' ${oldfile} > ${newfile}

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: use of VI editor in script

Just add the line of,

rm -f ${oldfile}

in above two solutions.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: use of VI editor in script

Hello,

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
"A ship in the harbor is safe, but that is not what ships are built for"
Arunvijai_4
Honored Contributor

Re: use of VI editor in script

Hello yogesh,

This should be useful, http://www.student.northpark.edu/pemente/sed/sed1line.txt

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
James R. Ferguson
Acclaimed Contributor

Re: use of VI editor in script

Hi:

Try this:

perl -naF -e '$old=$ARGV;if ($.==1) {open(FH,">",$F[0]);next};print FH' filename

Regards!

...JRF...
Bharat Katkar
Honored Contributor
Solution

Re: use of VI editor in script

This works fine for me.

script 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
#


You need to know a lot to actually know how little you know
James R. Ferguson
Acclaimed Contributor

Re: use of VI editor in script

Hi (again):

...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...
Bharat Katkar
Honored Contributor

Re: use of VI editor in script

hurry ......
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,
You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: use of VI editor in script

I feel JRF is shown the best. :)

#!/bin/sh
oldfile=abc
newfile=$(head -1 ${oldfile})
perl -ne 'print if ($. != 1)' ${oldfile} > ${newfile}
rm -f ${oldfile}

# END
exit 0

--
Muthu
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

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.

yogesh_4
Regular Advisor

Re: use of VI editor in script

Hi All,
Thanks for all help. I have used user solution and it resolved my problem.
Many thanks for your help.