2

I need a script to disassemble and reconstruct a file, deleting a line somewhere in it, and inserting a few lines in its place.

So A) I need a command that selects everything before that line, but not that line, and nothing after that line, and B) I need one that selects everything after that line, but not that line, only after it...

  • How can that particular line be specified? – sudodus Mar 18 '17 at 12:14
  • or some other way to edit a file, deleting one line somewhere in it, replacing it with a more then one line entry... – RPBCACUEAIIBH Mar 18 '17 at 12:15
  • well you can read it with cat file.sh | grep line... but that reads the line, not text before it, or after it... – RPBCACUEAIIBH Mar 18 '17 at 12:16
  • the line editor sed might be used if you identfy the line with its content or line number. splitcan be used if you identify the line with its line number. Maybe sed would be the best alternative. – sudodus Mar 18 '17 at 12:17
  • 2
    Please edit your question and i) give us an example input file and the output you would expect from it and ii) explain why you would want to do this in the shell. it is possible, but much harder, slower and with awful syntax. There are much better tools to use than the shell. Are you open to solutions that are not pure shell? – terdon Mar 18 '17 at 13:18
4

Example using sed

The file lines:

line 1
line 2
line three
line 4

Script, that replaces the line with the content line three with three new lines:

sed  '/line three/ c\
This is a new line\nNext line\nLast new line' lines

where \n 'newline' separates the new lines.

Output:

line 1
line 2
This is a new line
Next line
Last new line
line 4

See this link for details about sed,

www.grymoire.com/Unix/Sed.html

| improve this answer | |
  • I needed to specify the -i option to actually edit the file instead of displaying it, but it worked... – RPBCACUEAIIBH Mar 18 '17 at 15:08
  • I'm glad it works :-) Please tell us more about how you are using this script - How big is the file, that you are modifying and how do you identify the line to be replaced (and can you be sure that it will identify one and only one line)? – sudodus Mar 18 '17 at 15:16
0

Suppose you have a file named in.txt with the content:

one
two
three
four
five
And you also have a file named middle.txt:
drei
trois
If you want to replace "three" in the first file with the content of the second file, you can write a something like this into a file replace.awk:

#!/usr/bin/awk -f
{
    if ($0 == "three") {
        file="middle.txt";
        while ((getline  0) {
            print;
        }
    } else {
        print;
    }
}

Then make it executable:

chmod +x replace.awk

And run it:

./replace.awk < in.txt

The result will be:

one
two
drei
trois
four
five
| improve this answer | |
  • 1
    Note that your match will also insert the lines if the current line is, for example twenty-three or one two three or foothree or anything else matching three. It would be better to use $1 == "three" or, even better, "$0 == "three". – terdon Mar 18 '17 at 13:20
  • You're right, @terdon. I fixed the condition, thanks! – Cos64 Mar 18 '17 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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