Linux 如何在文件中每一行的开头添加一个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13586349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:53:28  来源:igfitidea点击:

How can I prepend a string to the beginning of each line in a file?

linuxbashsed

提问by bobbyrne01

I have the following bash code which loops through a text file, line by line .. im trying to prefix the work 'prefix' to each line but instead am getting this error:

我有以下 bash 代码,它逐行循环遍历文本文件。

rob@laptop:~/Desktop$ ./appendToFile.sh stusers.txt kp
stusers.txt
kp
./appendToFile.sh: line 11: /bin/sed: Argument list too long
[email protected],passw0rd

This is the bash script ..

这是bash脚本..

#!/bin/bash

file=
string=

echo "$file"
echo "$string"

for line in `cat $file`
do
    sed -e 's/^/prefix/' $line
    echo "$line"
done < $file

What am i doing wrong here?

我在这里做错了什么?

Update: Performing head on file dumps all the lines onto a single line of the terminal, probably related?

更新:执行文件头将所有行转储到终端的单行上,可能相关?

rob@laptop:~/Desktop$ head stusers.txt
rob@laptop:~/Desktop$ ouse.com,passw0rd

采纳答案by nullrevolution

a one-line awk command should do the trick also:

一行 awk 命令也应该可以解决这个问题:

awk '{print "prefix" 
while read -r line; do
do
    echo "$line" | sed -e 's/^/prefix/'
done < $file
}' file

回答by William Pursell

Instead of the for loop, it is more appropriate to use while read...:

而不是 for 循环,使用更合适while read...

sed -e 's/^/prefix/' $file

But you would be much better off with the simpler:

但是使用更简单的方法会更好:

perl -p -e's/^/prefix' filename

回答by Andy Lester

A Perl way to do it would be:

Perl 的方法是:

perl -p -e'$_ = "prefix $_"' filename

or

或者

while IFS= read -r line; do
    echo "prefix$line"
done < filename

In either case, that reads from filenameand prints the prefixed lines to STDOUT.

在任何一种情况下,它都会读取filename前缀行并将其打印到 STDOUT。

If you add a -iflag, then Perl will modify the file in place. You can also specify multiple filenames and Perl will magically do all of them.

如果您添加一个-i标志,那么 Perl 将修改该文件。您还可以指定多个文件名,Perl 会神奇地完成所有这些。

回答by glenn Hymanman

You don't need sed, just concatenate the strings in the echo command

您不需要 sed,只需连接 echo 命令中的字符串

for line in `cat file`; ...

Your loop iterates over each wordin the file:

您的循环遍历文件中的每个单词

sed -e 's/^/prefix/' $line

回答by Nick Petersen

Concerning your original error:

关于您的原始错误:

./appendToFile.sh: line 11: /bin/sed: Argument list too long

./appendToFile.sh:第 11 行:/bin/sed:参数列表太长

The problem is with this line of code:

问题在于这行代码:

echo $line | sed -e 's/^/prefix/'

$linein this context is file namethat sedis running against. To correct your code you should fix this line as such:

$line在这种情况下是文件名sed的运行反对。要更正您的代码,您应该像这样修复此行:

while read -r line; do  echo "PREFIX " $line; done < $file

(Also note that your original code should not have the < $fileat the end.)

(另请注意,您的原始代码不应< $file以 结尾。)

William Pursell addresses this issue correctly in both of his suggestions.

William Pursell 在他的两个建议中都正确地解决了这个问题。

However, I believe you have correctly identified that there is an issue with your original text file. dos2unixwill not correct this issue, as it only strips the carriage returns Windows sticks on the end of lines. (However, if you are attempting to read a Linux file in Windows, you would get a mammoth line with no returns.)

但是,我相信您已经正确地识别出原始文本文件存在问题。 dos2unix不会更正这个问题,因为它只会去掉行尾的回车符。(但是,如果您尝试在 Windows 中读取 Linux 文件,您会得到一个没有返回的庞大行。)

Assuming that it is not an issue with the end of line characters in your text file, William Pursell's, Andy Lester's, or nullrevolution's answers will work.

假设文本文件中的行尾字符没有问题,William Pursell、Andy Lester 或 nullrevolution 的答案将起作用。

A variation on the while read...suggestion:

while read...建议的变体:

while read -r line; do echo "kp" $line; done < stusers.txt

This could be run directly from the shell (no need for a batch / script file):

这可以直接从 shell 运行(不需要批处理/脚本文件):

sed -e 's/^/prefix/' $file

回答by Hyman

The entire loop can be replaced by a single sed command that operates on the entire file:

整个循环可以用一个对整个文件进行操作的 sed 命令替换:

sed -e 's/^/prefix/' file.ext

回答by Ahmad Awais

Use sed. Just change the word prefix.

使用 sed。换个词就行了prefix

sed -e 's/^/prefix/' file.ext > file_new.ext

If you want to save the output in another file

如果要将输出保存在另一个文件中

##代码##