Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

File is:

#Welcome to the file
#this is a file and it ends on "exit 0"
#Here are unknown number of lines
exit 0

Needs to have these lines one after each other:

echo deadline > /sys/block/sda/queue/scheduler
echo 1 > /sys/block/sda/queue/iosched/fifo_batch

So it looks like this:

#Welcome to the file
#this is a file and it ends on "exit 0"
#Here are unknown number of lines
echo deadline > /sys/block/sda/queue/scheduler
echo 1 > /sys/block/sda/queue/iosched/fifo_batch
exit 0

PROBLEMS- command inside command inside command with many Lines \n

For now I have command sed -i 's/exit 0/echo deadline > /sys/block/sda/queue/scheduler\necho 1 > /sys/block/sda/queue/iosched/fifo_batch\nexit 0/gp' /usr/file but something is wrong in it - it doesn't work.

Update 1

"SLASHES" / to \/: I found the solution for slashes to use \/ and backslashes \\.

So now my command works sed -i 's/exit 0/echo deadline > \/sys\/block\/sda\/queue\/scheduler\necho 1 > \/sys\/block\/sda\/queue\/iosched\/fifo_batch\nexit 0/' /usr/file

But it needs to be modified cause now it finds all expressions but how so just that it finds a line having nothing else in it? So it doesn't replace whats in exit 0 in example files comment.

Update 2

At least line starting with ^: Found to make solution for this file sed 's/^exit 0/\necho deadline > \/sys\/block\/sda\/queue\/scheduler\necho 1 > \/sys\/block\/sda\/queue\/iosched\/fifo_batch\nexit 0/' /usr/file but in case text file has lines:

exit 0
exit 0 means exiting

Then cant use ^ to find line starting with expression.

So best way would be which I can't find how to find exact line containing just expression or find expression line and not all expressions?

Update 3

Exact line ^ & $: Tuns out ^ limits nothing to be on left side and $ nothing to be on right side so now working solution for 1. solution is sed -i 's/^exit 0$/\necho deadline > \/sys\/block\/sda\/queue\/scheduler\necho 1 > \/sys\/block\/sda\/queue\/iosched\/fifo_batch\nexit 0/' /usr/file

I want to learn at least 3 ways using 1 line commands. How to:

  1. replace found line with 3 lines with symbol containing command as mentioned
  2. found complete line and not just expression and add 2 lines before found
  3. add 2 lines 1 line before end
share|improve this question

closed as too localized by Luis Alvarado Mar 14 '13 at 16:57

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
I'm not quite sure what you're asking. Can you show us an example of what an initial and final file each might look like? –  ændrük Nov 13 '11 at 9:22
    
Please ask multiple questions independently, as separate questions. –  Prateek Nov 13 '11 at 9:35
    
Please frame your question precisely. It's not clear what "replace found line with 3 lines with symbols" means, since one expects "replace" to have only one corresponding "with" after it, like "replace A with B". –  Prateek Nov 13 '11 at 9:42
    
@Kangarooo - please can you confirm if the answers below resolve your issue. –  fossfreedom Feb 28 '12 at 9:46

2 Answers 2

These might work for you (they all achieve the same thing!):

 sed '/^exit 0/i\echo deadline > /sys/block/sda/queue/scheduler\necho 1 > /sys/block/sda/queue/iosched/fifo_batch' file

Search for line beginning exit 0 and insert 2 lines.

Or:

 sed '$i\echo deadline > /sys/block/sda/queue/scheduler\necho 1 > /sys/block/sda/queue/iosched/fifo_batch' file

Insert 2 lines before last line of file.

Or:

 sed '$c\echo deadline > /sys/block/sda/queue/scheduler\necho 1 > /sys/block/sda/queue/iosched/fifo_batch\nexit 0' file

Change last line of file to 3 lines.

share|improve this answer

For question 3: I assume you want to add some text between the last and last-but-one line in a file. Additionally, this text consists of 2 lines, but that doesn't matter. head -n -1 will extract all but the last line, and tail -n 1 will extract the last line. So one can do

head -n -1 file > outputfile
cat file_with_two_line_text >> outputfile
tail -n 1 file >> outputfile

This isn't very efficient as it scans 'file' twice, while one needs to scan it only once.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.