简体   繁体   English

如何将字符串添加到文件中每一行的开头?

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

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 代码,它逐行循环遍历文本文件。我试图为每一行添加前缀“prefix”,但却收到此错误:

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

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

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

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

$line in this context is file name that sed is running against. 此上下文中的$line是正在运行的sed的 文件名 To correct your code you should fix this line as such: 要更正代码,您应该修复此行:

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

(Also note that your original code should not have the < $file at 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. 但是,我相信您已正确识别原始文本文件存在问题。 dos2unix will not correct this issue, as it only strips the carriage returns Windows sticks on the end of lines. dos2unix不会纠正这个问题,因为它只会dos2unix Windows回车。 (However, if you are attempting to read a Linux file in Windows, you would get a mammoth line with no returns.) (但是,如果您尝试在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...建议:

while read -r line; do  echo "PREFIX " $line; done < $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

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

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

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. 在任何一种情况下,都从filename读取并将前缀行打印到STDOUT。

If you add a -i flag, then Perl will modify the file in place. 如果添加-i标志,那么Perl将修改该文件。 You can also specify multiple filenames and Perl will magically do all of them. 您还可以指定多个文件名,Perl将神奇地完成所有这些操作。

Instead of the for loop, it is more appropriate to use while read... : 而不是for循环,更适合while read...使用while read...

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

Use sed. 使用sed。 Just change the word prefix . 只需更改单词prefix

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

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`; ...
sed -i '1a\
Your Text' file1 file2 file3

A solution without sed/awk and while loops:没有 sed/awk 和 while 循环的解决方案:

xargs -n1 printf "$prefix%s" < "$file"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在bash文件夹中的每个文件的开头添加一个字符串? - How can I add a string to the beginning of each file in a folder in bash? 我如何在每行的开头添加添加文字? - how i can add Add text at the beginning of each line? 如何从文件的每一行中获取元素,并将它们添加到不同文件每一行的每个条目中? - How to take elements from each line of a file, and prepend them to each entry in each line of a different file? 如何将字符串添加到与我的模式匹配的目录中的每个文件? - How do I prepend a string to each file in directory that matches my pattern? 如何在 Linux 中尽快将文件中的列表项添加到另一个文件的每一行? - How can I prepend list items from a file to every line of another file as fast as possible in Linux? 如何在文件的每一行的开头均匀添加一组值? - How to evenly add a set of values at the beginning of each line in a file? 在每行的开头添加前缀字符串 - Add a prefix string to beginning of each line 将文件的完整路径放在文本文件的每一行之前 - Prepend full path of file to each line of a text file 围绕nc命令编写的bash脚本。 如何在每行之前添加文字? - A bash script written around the nc command. How do I prepend text before each line? 在 Bash 中,如何在文件的每一行之后添加一个字符串? - In Bash, how do I add a string after each line in a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM