Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a new line after a pattern is found in a file

Status
Not open for further replies.

gringomike

Technical User
Aug 6, 2003
148
GB
Hi all,

I'm trying to insert a new line after the text "sometext="" appears on a line of a file.

I've tried a few things using sed (including the lines below) but nothing has worked so far.

cat path/to/file/file1.txt |sed 's/^sometext="//' >> /path/to/file/a.txt

cat path/to/file/file1.txt |sed 's/^sometext="/"\n"/' >> /path/to/file/a.txt

Am I on the right lines here or am I way off the mark!?

Thanks

GM
 
The sed way:
sed '/^sometext=/a\
' /path/to/file1.txt > output
The awk way:
awk '{print}/^sometext=/{printf "\n"}' /path/to/file1.txt > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Thanks for the reply however I'm afraid this isn't quite the solution I was looking for.

I need the text that appears after sometext=" to be printed on the next line.

e.g. This is the original file format;

sometext="text1
text2
text3
text4"

I need it to display this;

sometext="
text1
text2
text3
text4"

Your solution gives me the following output;

sometext="text1

text2
text3
text4"


How do I insert a new line character halfway through a line?

Thanks

GM
 
If you have access to perl then
Code:
perl -e 'while (<>)
{
/^(sometext=)(.*)$/ and (print "$1\n$2\n"), next;
print;
}' < infile > outfile

Columb Healy
 
Code:
sed 's/sometext="/sometext="\
/'


HTH,

p5wizard
 
nawk '{sub(/^sometext="/,"&\n";print}' /path/to/file1.txt > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top