Choose a line-number to add line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Choose a line-number to add line
# 1  
Old 10-17-2013
Choose a line-number to add line

Hello guys,

I'm making a script to add visudo with this script.

Do you guys know if it's possible to add words to a line-number you want to.

Something like this:

Code:
echo "Adding words to line-number 16" >> /etc/sudoers # (options to add to line-number-16)?

Thanks!

Last edited by Scott; 10-17-2013 at 11:48 AM.. Reason: Code tags
# 2  
Old 10-17-2013
Code:
linenumber=16
sed "${linenumber}s/$/sometext/" file

will append sometext to line 16 of file.

EDIT: If you want to edit the file inplace then check if your sed supports -i. If not then you'll need to redirect to a temp file and move/copy the files around.
This User Gave Thanks to CarloM For This Post:
# 3  
Old 10-17-2013
You can do it with sed. i.e.
Code:
sed "16i\ 
newline goes here
" /etc/sudoers

EDIT: -i (if the option is available) will overwrite the original file. Always take a backup first!
EDIT 2: LOL CarloM and me are editing in sync Smilie
These 2 Users Gave Thanks to Scott For This Post:
# 4  
Old 10-17-2013
Thanks scott,

I also found a other option:

Code:
sed -i "3i HELLO" /etc/sudoers

EDIT: And thanks CarloM

Last edited by Scott; 10-17-2013 at 12:09 PM.. Reason: Please use code tags...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add line to previous line if not a number?

Hi, I am trying to compare 2 lists. However, one of these lists has to be taken from a.pdf file. When I copy the test into a .txt document there are formatting errors which I need to correct. The document is long (~10,000 lines) so I need to script the re-formatting. Currently my file looks... (9 Replies)
Discussion started by: carlr
9 Replies

2. Shell Programming and Scripting

Add line number to for loop?

> cat test.sh for t in `cat out.txt` do echo create directory data as "'"$t"';" done > output of out.txt is as below /oracle/SID/data1/file2_1/ /oracle/SID/data1/file1_1/ /oracle/SID/data1/file5_5/ /oracle/SID/data1/file_9/ /oracle/SID/data1/file_2/ when i run my test.sh... (6 Replies)
Discussion started by: crazy_max
6 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

add number in lines line by line in different files

I have a set of log files that are in the following format ======= set_1 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_2 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_3 ======== counter : 315 counter2: 204597 counter3: 290582 Is... (6 Replies)
Discussion started by: grandguest
6 Replies

7. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question