如何將字符串前置到文件中每行的開頭?

[英]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代碼,循環遍歷文本文件,逐行..我試圖將工作'前綴'作為前綴添加到每一行,但我得到此錯誤:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/11/27/72f53d0cd512f642922e39bb4e68ae02.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com