Add text at start and ending of every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add text at start and ending of every line
# 1  
Old 07-20-2015
Add text at start and ending of every line

Hi all,

Is there other way to Add text at start and ending of every line?

here my script:

Code:
cat file.txt |awk '{print "<p align=\"justify\">"$0"</p>"}'

but the problem they put including white spaces, I only need those line have a sentence or text not an skip all have empty string or have a white space
# 2  
Old 07-20-2015
Hello lxdorney,

If I understood correctly you need to skip those lines which are empty, then you can try following. It is good you are showing us what you have tried so far, please try to add your input file and expected output too. Will be helpful for us.
Code:
 awk '($0 !~ /^$/){print "<p align=\"justify\"> " $0 " </p>"}'  Input_file


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-20-2015
If you want to exclude lines containing <TAB>s and spaces as well, try
Code:
awk '!/^[\t ]*$/ {print "<p align=\"justify\"> " $0 " </p>"}'  file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-20-2015
The following takes advantage of awk's autosplit on whitespace.
Code:
awk 'NF {print "<p align=\"justify\"> " $0 " </p>"}' file.txt

If you want to keep the blank lines:
Code:
awk 'NF {$0 = "<p align=\"justify\"> " $0 " </p>"} {print}' file.txt

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-20-2015
Quote:
cat file.txt |awk '{print "<p align=\"justify\">"$0"</p>"}'
You don't need to use cat. Awk can read files on its own.

Here's another way without Awk.
Code:
 perl -nle 'print "<p align=\"justify\">$_</p>" unless /^$/' file.txt

This User Gave Thanks to Aia For This Post:
# 6  
Old 07-20-2015
Thanks for the help,

I tried above scripts but the output is all same

Code:
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify"> Hello World ^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">Hello World1^M</p>
<p align="justify">^M</p>
<p align="justify">Hello World2^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>
<p align="justify">Hello World3^M</p>
<p align="justify">^M</p>
<p align="justify">^M</p>

I have a text file:

#cat text.txt

Code:




Hello World



Hello World1

Hello World2



Hello World3

and heres what I want to Achieve:

Code:




<p align="justify"> Hello World</p>



<p align="justify">Hello World1</p>

<p align="justify">Hello World2</p>



<p align="justify">Hello World3</p>

# 7  
Old 07-21-2015
you've a DOS formatted file then. first convert it to unix, or amend the script like so:

Code:
awk '{sub(/\r$/,"")}NF {print "<p align=\"justify\"> " $0 " </p>"}' file.txt

This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

4. Shell Programming and Scripting

Deleting text start at 2nd line....

How can I delete the first '2' characters in the 2nd line and every fourth line thereafter (Means line no.s 2, 6, 10...so on). For example if the following is my Input treieieiei trrpepepepep tyeueueue tyeueueeu tyeyeyeye tryryry tyeyey tyrtyty tytyty tututu tututututu Output... (8 Replies)
Discussion started by: cs_novice
8 Replies

5. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

6. Shell Programming and Scripting

Remove ending text

Hello, I am working with a list that contains a large number of files listed by their absolute path. I am trying to determine a way to delete the file name at the end of each line, therefore leaving just the directory path. For example, I'd like to go from: /home/something/file... (2 Replies)
Discussion started by: omnivir
2 Replies

7. Shell Programming and Scripting

Convert directory of text files to Unix/Linux Line Ending

I need help converting a directory of *.txt with Windows line ending to UTF-8 character encoding and Unix/Linux line ending. (9 Replies)
Discussion started by: chipperuga
9 Replies

8. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

9. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

10. Shell Programming and Scripting

Appending line ending with '}" to new line

Hello masters. I have a rather simple problem but its been killing me. I have a file "x" with only 1 line inside it. The line looks something like Now this is only part of the line. Its actually about 4000 characters. What i need to do is whenever there is a "}", i need to append the next... (4 Replies)
Discussion started by: aismann
4 Replies
Login or Register to Ask a Question