LinuxQuestions.org
Help answer threads with 0 replies.
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, 05: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 05:43 PM. Reason: spelling
 
Old 07-19-2012, 05:57 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
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, 07:25 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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, 09:00 PM   #4
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
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, 10:16 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 01:06 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 05: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:58 AM
[SOLVED] add a word in end of lines & edit with +1 in every line 1Volt Linux - Newbie 12 11-11-2011 04: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 03:47 PM
Add comma to end of lines in text file Johng Programming 9 08-21-2010 05:15 AM
add text to end of line? tatoosh Linux - Newbie 8 07-31-2009 04:28 AM


All times are GMT -5. The time now is 06:50 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration