sed command to Insert a line before the last four lines of the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed command to Insert a line before the last four lines of the file
# 1  
Old 09-18-2015
sed command to Insert a line before the last four lines of the file

By using sed command, How to insert a new line before the last four lines of the file.

Code:
Old Line
Old Line
NEW LINE!
Old Line
Old Line
Old Line
Old Line


Last edited by wridler; 09-18-2015 at 07:36 AM.. Reason: code tags
# 2  
Old 09-18-2015
Hi,
Example:
Code:
$ cat file.txt
Old Line
Old Line
Old Line
Old Line
Old Line
Old Line
$ tac file.txt | sed -e '5iNEW LINE!' | tac
Old Line
Old Line
NEW LINE!
Old Line
Old Line
Old Line
Old Line

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 09-18-2015
Is this a homework assignment?

Homework and coursework questions can only be posted in this forum under special homework rules.

If this is homework, please repost in the Homework and Coursework Forum. If not, please explain why you are trying to do this.

Hi disedorgue,
I am hiding your response until we get an answer to the homework question.
# 4  
Old 09-18-2015
Hi Don,

This is not an either homework or coursework assignment. I am playing with sed commands and so want to know that how we can read the lines from the bottom using sed.

Please let me know if still I need to post my question to homework forum?


Thanks
# 5  
Old 09-18-2015
Not sure if this can be done with sed alone. Try
Code:
LN=4
tac file | sed ${LN}a"NEW LINE" | tac
Old Line
Old Line
NEW LINE
Old Line
Old Line
Old Line
Old Line

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-18-2015
Thanks Rudi it worked..
# 7  
Old 09-18-2015
If you're trying to learn how to use sed, you should also learn to recognize things that sed was not designed to do (like counting lines backwards from the end of a file). A tool like ed or ex could handle an assignment like this without needing to invoke tac twice to get data in a format that sed can handle... For instance, if you want to update the contents of the file:
Code:
#!/bin/ksh
LN=4
IP=$((LN - 1))
ed -s file.txt <<-EOF
        .-${IP}i
	.
	w
	q
EOF

or if you just want to print the updated contents (so you can redirect the output to a different file or just view the update):
Code:
#!/bin/ksh
LN=4
IP=$((LN - 1))
ed -s file.txt <<-EOF
        .-${IP}i
NEW LINE!
        .
        1,$ p
        Q
EOF

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert charactera in 1st position of specific lines using vi editor or sed command

Dear all, i am having text file like below surya rama ranga laxman rajesh reddy i want add string (OK) before a text from line 3 to 5 the result will be surya rama OK ranga OK laxman OK rajesh reddy (1 Reply)
Discussion started by: suryanarayana
1 Replies

2. Shell Programming and Scripting

sed insert new line does not update file

Hi all, I have a file called "text.cpp" with the first line of "1" afterwards I tried in Ubuntu to type the following sed '12iasdasdasdasdsad' test.cpp > output.txt however when I tried to see the result of output.txt #cat output.txt 1 why is the line 12 is not updated to the... (6 Replies)
Discussion started by: peuceul
6 Replies

3. Shell Programming and Scripting

Insert output from a file to beginning of line with sed

Hi I've been trying to search but couldn't quite get the answer I was looking for. I have a a file that's like this Time, 9/1/12 0:00, 1033 0:10, 1044 ... 23:50, 1050 How do I make it so the file will be like this? 9/1/12, 0:00, 1033 9/1/12, 0:10, 1044 ... 9/1/12, 23:50, 1050 I... (4 Replies)
Discussion started by: diesel88
4 Replies

4. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

5. Shell Programming and Scripting

Insert shell command into first line of output file

How can I insert the command executed on the shell into the first line of my output file? For example if I execute; zcat *.gz |grep “User5501” > users.out How can I make my users.out look like; zcat *.gz |grep “User5501” > users.out User5501 PA User5501 UA User5501 ZA... (3 Replies)
Discussion started by: lewk
3 Replies

6. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

7. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

8. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

9. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

10. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies
Login or Register to Ask a Question