Reputation: 4427
For example , A text file has:
dfsdfsd
f
dsf
dsf
dsf
dsf
sdafadfdasfdsfd
sf
sdfasdfdasfdsfsdf
sd
fsdfdsaf
Then , i would like to find a way to obtain the line number of sf and insert a paragraph before sf
Are there any ways to doing it in bash programming ???thanks
Upvotes: 3
Views: 1473
Reputation: 247182
sed
has more commands than just s///
. To insert text before each matching line:
sed '/pattern/ i \
text to be \
inserted goes \
here'
Upvotes: 1
Reputation: 12613
If you want to find out the line number, you could use grep -n
.
If you just want to insert a line on the preceding line, you could use sed
like this:
sed "s/sd/paragraph\nsd/" file
This will insert the text "paragraph" above the line with "sd".
To only do this for the first match:
sed "0,/sd/ { s/sd/paragraph\nsd/ }" file
Here, we only match lines until the first matching "sd", so any later sd's will not match.
Upvotes: 1
Reputation: 12075
Possibly a regex replace, replacing \nsf\n
with \n\nsf\n
, or something similar depending on your specific requirements. Doesn't get you the actual line number, but if all you want to do is insert a paragraph, that may not be necessary.
Upvotes: 0