Sponsored Content
Top Forums Shell Programming and Scripting insert one line inbetween file with use of numbers Post 302636207 by susindram on Monday 7th of May 2012 09:12:15 AM
Old 05-07-2012
Hi Pokerio,,

Your code is working..I have added variable accoeding to your code.But getting error while giving valriable...

Code:
$ more master.sh
#!/bin/ksh
echo "Add new user account to master "
echo -n "masterlogin: " ;read masterlogin
Search_masterlogin=`grep -i $masterlogin passwd.master|awk -F: '{print $1}'`
echo "Value of Search_masterlogin :$Search_masterlogin"
if [ "$Search_masterlogin" = "$masterlogin" ] ;then
echo "master user account found"
exit 0
fi
####### Identify available free UID ###################
Available_UID=`grep -n "7000" passwd.master |awk -F":" '{print $1-3 "," $1+3 "p"}' |xargs -t -i sed -n {} passwd.master`
echo $Available_UID
echo -n "Enter the previous UID: " ; read previousUID
echo -n "Enter the master UID: " ;read masterUID
echo -n "Enter the master GID:" ;read masterGID
echo -n "Enter the user fullname :" ;read username
echo -n "Enter the home directory :" ;read homedirectory
echo -n "Enter the shell :" ;read shell
echo -n "Enter the server name :" ; read servername
finel_update=`$masterlogin:x:$masterUID:$masterGID:$username:$homedirectory:$shell:$servername
cat passwd.master |sed ' /$previousUID/ a\$finel_update '
$

please find the below for passwd.master file..I am adding the line above the 7000 and below the 6850.

Code:
$ more passwd.master
jamishra:x:6846:8579:jamishra:/app/home/jamishra:/bin/bash
ajaygupt:x:6847:8579:ajaygupt:/app/home/ajaygupt:/bin/bash
redwards:x:6848:8134:Edwards, Ronald :/home/users/redwards
krisvasu:x:6849:8360:Vasu, Krishnadas:/home/readgrp/krisvasu:/bin/ksh
malam:x:6850:9014:Alam, Mahfooz:/app/home/malam:/bin/ksh
#---- range 7000 - SysAdmin applications
cricka:x:7001:8011:AT:/atmdev/home/crickatm:/bin/ksh
cricki:x:7002:8012:Int:/infdev/home/crickinf:/bin/ksh
oracle:x:7003:2002:Oracle:/var/home/oracle:/bin/ksh
dba:x:7004:2002:Oracle DBA:/var/home/dba:/bin/ksh:
named:x:7005:7005:Named:/var/named:/bin/false

$

Thanks,
Susi.S

---------- Post updated at 08:12 AM ---------- Previous update was at 05:48 AM ----------

Hi All,

Finally i came to know whats issue...But only one problem to me script is executing and its shows in script updating..but its not reflecting in file...

Don't no whats issue

Code:
 Script :-

#!/bin/ksh
echo "Add new user account to master "
echo -n "masterlogin: " ;read masterlogin
Search_masterlogin=`grep -i $masterlogin passwd.master|awk -F: '{print $1}'`
echo "Value of Search_masterlogin :$Search_masterlogin"
if [ "$Search_masterlogin" = "$masterlogin" ] ;then
echo "master user account found"
exit 0
fi
####### Identify available free UID ###################
Available_UID=`grep -n "7000" passwd.master |awk -F":" '{print $1-3 "," $1+3 "p"}' |xargs -t -i sed -n {} passwd.master`
echo $Available_UID
echo -n "Enter the previous UID: " ; read previousUID
echo -n "Enter the master UID: " ;read masterUID
echo -n "Enter the master GID:" ;read masterGID
echo -n "Enter the user fullname :" ;read username
echo -n "Enter the home directory :" ;read homedirectory
echo -n "Enter the shell :" ;read shell
echo -n "Enter the server name :" ; read servername
finel_update="$masterlogin:x:$masterUID:$masterGID:$username:$homedirectory:$shell:$servername"
sed ' /'$previousUID'/ a\'$finel_update' ' passwd.master
$


Script output :-

Enter the previous UID: 6850
Enter the master UID: 6851
Enter the master GID:999
Enter the user fullname :susindran
Enter the home directory :/home/ssuruli
Enter the shell :/bin/ksh
Enter the server name :machard
jamishra:x:6846:8579:jamishra:/app/home/jamishra:/bin/bash
ajaygupt:x:6847:8579:ajaygupt:/app/home/ajaygupt:/bin/bash
redwards:x:6848:8134:Edwards, Ronald :/home/users/redwards:/bin/ksh
krisvasu:x:6849:8360:Vasu, Krishnadas:/home/readgrp/krisvasu:/bin/ksh
malam:x:6850:9014:Alam, Mahfooz:/app/home/malam:/bin/ksh
ssuruli:x:6851:999:susindran:/home/ssuruli:/bin/ksh:
#---- range 7000 - SysAdmin applications


But its not reflecting in file :-

$ more passwd.master
jamishra:x:6846:8579:jamishra:/app/home/jamishra:/bin/bash
ajaygupt:x:6847:8579:ajaygupt:/app/home/ajaygupt:/bin/bash
redwards:x:6848:8134:Edwards, Ronald :/home/users/redwards:/bin/ksh
krisvasu:x:6849:8360:Vasu, Krishnadas:/home/readgrp/krisvasu:/bin/ksh:
malam:x:6850:9014:Alam, Mahfooz:/app/home/malam:/bin/ksh


Any idea Smilie

Thanks,
Susi.S

Last edited by susindram; 05-07-2012 at 10:15 AM.. Reason: marked in red
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies

2. Shell Programming and Scripting

Insert rows based on line numbers

Can I insert rows based on line numbers. Say If I need to insert 1 or more rows in a file from line number 10. Can I do that in UNIX I have a file something like A B C D E F After row C, I wanted to add 2 records as X and Y. I have the line number after C as my reference. Can I... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

3. Shell Programming and Scripting

awk filter numbers inbetween

Hi, I have a file which has a number in each line ( i think they are strings ) I will have a $first and $last variable, which are strings but contains only numbers. Also a file $f, I want to filter out the lines in $f with only numbers in between $first and $last. Do I need to consider the... (2 Replies)
Discussion started by: a27wang
2 Replies

4. Shell Programming and Scripting

How to Remove the new line character inbetween a record

I have a file, in which a single record spans across multiple lines, File 1 ==== 14|\n leave request \n accepted|Yes| 15|\n leave request not \n acccepted|No| I wanted to remove the '\n charecters. I used the below code (foudn somewhere in this forum) perl -e 'while (<>) { if... (1 Reply)
Discussion started by: machomaddy
1 Replies

5. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

6. Shell Programming and Scripting

Delete last two line inbetween pattern

Hi I had the text file as below and i wand to keep only 4 lines from tag :7023: to next : (i.e new tag start) including tag lines. any solution ? :7023:TEST LINE NUMBER 1101010021442 TESTING HI HERE PLEASE HELP ME OUT AC 20101906523 :7028:OUR (12 Replies)
Discussion started by: sandipgawale
12 Replies

7. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

8. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

9. Shell Programming and Scripting

How to insert random numbers into each line?

I have a file contains thousands of lines. I want to insert n random numbers into each line at specific position. Like this: 0 22…… 1 33…… …… …… I want to insert 3 random numbers from position 2 to 4 into each line. 0 325 22…… 1 685 33…… …… …… Please use CODE tags when... (8 Replies)
Discussion started by: hhdzhu
8 Replies
All times are GMT -4. The time now is 06:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy