English 中文(简体)
壳变量线末
原标题:End of line in shell variables

因此,我有testfile

Line one
Another line
and this is the third line

我的发言稿读到这一档案中,有点不足,我最后还有一个可以包含的变量。 of

filevar=$(cat testfile)

(这里重要的是,我不能直接查阅档案)。

我将使用该变量的内容生成一个超文本代码,而我必须做的一件事是在每个行文末尾添加<代码><br>。 问题在于,在我瓦尔似乎有过任何电子运输工具:

echo $filevar
Line one Another line and this is the third line

How do I read the file properly to keep the EOLs? Once I have that I can simply sed s/$/<br>/g, but till then...

感谢!

最佳回答

http://www.ohchr.org。 这将发出一个单一的论点来回应,然后,你可以把这一论点推向前进。

顺便说一句,将视之为3行,因此你不需要这种选择。 与我合作(现金和cy子):

echo "$filevar" | sed  s/$/<br>/ 
问题回答

如何改变爱幼学校?

#!/bin/bash

IFS=""
filevar=$(cat test)
echo $filevar

产出:

Line one
Another line
and this is the third line

我无法理解,为什么你需要把档案翻译成变数。 为什么不只要求你这样做:

sed  s|$|<br/>|  testfile 

<><>UPDATE:

If you really want to get the EOL back in your variable. Try this (notice the quotes):

echo "$filevar"

但我仍然可以理解,why You can cat。 查阅文件

作为一个解决办法,我要提出以下文字:

while read LINE
do
  echo ${LINE}  <br />    # Implement your core logic here.
done < testfile

您需要制定<代码>IFS的变量,以仅包含一条新线,然后参照<代码>.var>/code><> 可变>,无<>>>。

$ filevar= Line one
Another line
and this is the third line 

$ for word in $filevar; do echo "$word<br>"; done
Line<br>
one<br>
Another<br>
line<br>
and<br>
this<br>
is<br>
the<br>
third<br>
line<br>

$ for word in "$filevar"; do echo "$word<br>"; done
Line one
Another line
and this is the third line<br>

$ (IFS=$ 
 ; for word in $filevar; do echo "$word<br>"; done)
Line one<br>
Another line<br>
and this is the third line<br>




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签