Hi all,
I'm having a small problem which I'm struggling to find answer to. My main problem is adding two (or more) lines under a certain line in a file.
To keep it a straight forward example as possible, I have a file called "sample.txt" which contains lines as follows:
entry 1/1
entry 2/1
entry 3/1
entry 4/1
entry 5/1
#Script looks like so:
#!/bin/bash
FILE="sample.txt"
cat $FILE | sed -e '/^entry/a\
note 1
'
# End of script
Output is like this:
entry 1/1
note 1
entry 2/1
note 1
entry 3/1
note 1
entry 4/1
note 1
entry 5/1
note 1
This is fine ... however, the two problems I have are:
1- How do I make this work by introducing the new text to be added as a variable
ie If at top of script I put TXT="note 1" and make script look like this:
FILE="sample.txt"
TXT="note 1"
cat $FILE | sed -e '/^entry/a\
$TXT
'
The output comes out wrong like follows:
entry 1/1
$TXT
etc... (I've tried different quote marks without luck)
2- How do I put more than one entry in to get output like so:
entry 1/1
note 1
note 2
entry 2/1
note 1
note 2
entry 3/1
note 1
note 2
I've tried various things but not winning *sigh*
Any tips or pointers to previous posts that I'm not finding is appreciated.
Thanks... TJ
I'm having a small problem which I'm struggling to find answer to. My main problem is adding two (or more) lines under a certain line in a file.
To keep it a straight forward example as possible, I have a file called "sample.txt" which contains lines as follows:
entry 1/1
entry 2/1
entry 3/1
entry 4/1
entry 5/1
#Script looks like so:
#!/bin/bash
FILE="sample.txt"
cat $FILE | sed -e '/^entry/a\
note 1
'
# End of script
Output is like this:
entry 1/1
note 1
entry 2/1
note 1
entry 3/1
note 1
entry 4/1
note 1
entry 5/1
note 1
This is fine ... however, the two problems I have are:
1- How do I make this work by introducing the new text to be added as a variable
ie If at top of script I put TXT="note 1" and make script look like this:
FILE="sample.txt"
TXT="note 1"
cat $FILE | sed -e '/^entry/a\
$TXT
'
The output comes out wrong like follows:
entry 1/1
$TXT
etc... (I've tried different quote marks without luck)
2- How do I put more than one entry in to get output like so:
entry 1/1
note 1
note 2
entry 2/1
note 1
note 2
entry 3/1
note 1
note 2
I've tried various things but not winning *sigh*
Any tips or pointers to previous posts that I'm not finding is appreciated.
Thanks... TJ