LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-19-2012, 04:42 PM   #1
smithy2010
Member
 
Registered: May 2010
Location: UK
Distribution: OpenSuse 64 bit
Posts: 45

Rep: Reputation: 15
Smile add text at the beginning and at the end of lines in a file


Hello all,

I have one text file with about 15 lines of data. I am trying to write a script that would add this <p> at the beginning and at the end of a line, from line 5 to line 15, but ignore first 5 lines of text.

I had a go with the sed command but I just don’t know how to do it, can anyone advice of ways to achieve this please?

Thank you in advance

D.

Last edited by smithy2010; 07-19-2012 at 04:43 PM. Reason: spelling
 
Old 07-19-2012, 04:57 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
ash-4.2$ sed '5,15 s/.*/<p>&<p>/' test
      361459	447394	  CHL1
      290282	290282	  CHL1
      361459	447394	  CHL1
      361459	447394	  CHL1
<p>      178352861	178363529 AGA<p>
<p>      178352861	178363529 AGA<p>
<p>      178363657	178363657 AGA<p>
<p>361459	447394	  CHL1<p>
<p>      290282	290282	  CHL1<p>
<p>      361459	447394	  CHL1<p>
<p>      361459	447394	  CHL1<p>
<p>      178352861	178363529 AGA<p>
<p>      178352861	178363529 AGA<p>
<p>      178363657	178363657 AGA<p>
<p> 178363657	178363657 AGA<p>
 
Old 07-19-2012, 06:25 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
For that sot of thing, bash should suffice.
Code:
#!/bin/bash
count=0
while read -r line
do
  if [ ${count} -lt 5 ]
  then
    echo ${line}
  else
    echo "<p>${line}</p>"
  fi
  count=$((++count))
done < ${1:-/dev/stdin}
Example:
Code:
$ ls -l | bash smithy2010
total 92
-rw-rw-r-- 1 Peter Peter 28500 Feb 27 14:26 Brews ohare.txt
-rwxrwxr-x 1 Peter Peter 6551 Feb 27 14:26 enum
-rw-rw-r-- 1 Peter Peter 222 Feb 27 14:26 enum.c
-rw-rw-r-- 1 Peter Peter 23947 Feb 27 14:26 output
<p>-rwxrwxr-x 1 Peter Peter   220 Feb 27 14:26 rock1961.gawk</p>
<p>-rw-rw-r-- 1 Peter Peter    57 Feb 27 14:26 rock1961.txt</p>
<p>-rwxr-xr-x 1 Peter Peter   673 Feb 27 14:26 scramble</p>
<p>-rw-rw-r-- 1 Peter Peter   173 Jul 19 16:19 smithy2010</p>
<p>-rwxrwxr-x 1 Peter Peter  1255 Feb 27 14:26 squash</p>
<p>-rw-rw-r-- 1 Peter Peter  1873 Feb 27 14:26 swap</p>
<p>-rw-rw-r-- 1 Peter Peter  1669 Feb 27 14:26 tuxlux.awk</p>
 
Old 07-19-2012, 08:00 PM   #4
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
Using awk

Code:
$ awk '{ if (NR>=5 && NR<=15) print "<p>"$0"</p>"; else print $0}' test
1
2
3
4
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p>15</p>
 
Old 07-19-2012, 09:16 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Alternative sed (although I think the tag at the end needs the slash):
Code:
sed -r 's/^|$/<p>/g' file
Awk can also be shortened:
Code:
awk 'NR>=5 && NR<=15{$0="<p>"$0"</p>"}1' file
 
Old 07-20-2012, 12:06 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Similarly, using ed:

Code:
printf '%s\n' '5,15 s/.*/<p>&<p>/' ',p' | ed -s infile
',p' prints the altered file to stdout. Change to 'w' to write it back to the original file.

And as grail mentioned, if the end tag is actually "</p>" then you'll have to backslash escape the forward slash.


How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
Old 07-22-2012, 04:07 AM   #7
smithy2010
Member
 
Registered: May 2010
Location: UK
Distribution: OpenSuse 64 bit
Posts: 45

Original Poster
Rep: Reputation: 15
Hello all,

Thank you very much for your reply, I found this command to be the most suitable sed -i '5,15 s/.*/<p>&<p>/' file name.


Best wishes


D.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Add lines end of file above the end comments bkone Programming 2 02-27-2012 09:58 AM
[SOLVED] add a word in end of lines & edit with +1 in every line 1Volt Linux - Newbie 12 11-11-2011 03:03 PM
[SOLVED] Using Vim to add something to the end of a series of a lines Vim-Newbie Linux - Newbie 3 05-23-2011 02:47 PM
Add comma to end of lines in text file Johng Programming 9 08-21-2010 04:15 AM
add text to end of line? tatoosh Linux - Newbie 8 07-31-2009 03:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration