如何将字符串前置到文件中每行的开头?

时间:2022-03-17 06:50:26

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
115000_210org@house.com,passw0rd

This is the bash script ..

这是bash脚本..

#!/bin/bash

file=$1
string=$2

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?

更新:执行head on file将所有行转储到终端的一行,可能是相关的?

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

7 个解决方案

#1


31  

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

一行awk命令也可以做到这一点:

awk '{print "prefix" $0}' file

#2


9  

Concerning your original error:

关于你原来的错误:

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

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

The problem is with this line of code:

问题在于这行代码:

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

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

$line in this context is file name that sed is running against. To correct your code you should fix this line as such:

此上下文中的$ line是sed正在运行的文件名。要更正代码,您应该修复此行:

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

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

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

(另请注意,您的原始代码最后不应包含<$文件。)

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. dos2unix will 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回车。 (但是,如果您尝试在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读取的变化...建议:

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

读取-r线;做echo“PREFIX”$ line;完成<$ file

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

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

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

读取-r线;做echo“kp”$ line;完成

#3


6  

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

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

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

#4


3  

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

而不是for循环,更适合在读取时使用...:

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

But you would be much better off with the simpler:

但是用更简单的方法你会好得多:

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

#5


3  

A Perl way to do it would be:

Perl的方法是:

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

or

要么

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

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

在任何一种情况下,都从文件名读取并将前缀行打印到STDOUT。

If you add a -i flag, 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将神奇地完成所有这些操作。

#6


0  

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

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

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

Your loop iterates over each word in the file:

你的循环迭代文件中的每个单词:

for line in `cat file`; ...

#7


0  

Use sed. Just change the word prefix.

使用sed。只需更改单词前缀即可。

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

If you want to save the output in another file

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

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

#1


31  

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

一行awk命令也可以做到这一点:

awk '{print "prefix" $0}' file

#2


9  

Concerning your original error:

关于你原来的错误:

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

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

The problem is with this line of code:

问题在于这行代码:

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

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

$line in this context is file name that sed is running against. To correct your code you should fix this line as such:

此上下文中的$ line是sed正在运行的文件名。要更正代码,您应该修复此行:

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

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

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

(另请注意,您的原始代码最后不应包含<$文件。)

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. dos2unix will 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回车。 (但是,如果您尝试在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读取的变化...建议:

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

读取-r线;做echo“PREFIX”$ line;完成<$ file

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

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

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

读取-r线;做echo“kp”$ line;完成

#3


6  

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

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

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

#4


3  

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

而不是for循环,更适合在读取时使用...:

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

But you would be much better off with the simpler:

但是用更简单的方法你会好得多:

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

#5


3  

A Perl way to do it would be:

Perl的方法是:

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

or

要么

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

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

在任何一种情况下,都从文件名读取并将前缀行打印到STDOUT。

If you add a -i flag, 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将神奇地完成所有这些操作。

#6


0  

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

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

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

Your loop iterates over each word in the file:

你的循环迭代文件中的每个单词:

for line in `cat file`; ...

#7


0  

Use sed. Just change the word prefix.

使用sed。只需更改单词前缀即可。

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

If you want to save the output in another file

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

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