Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

sed

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:

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 ..

#!/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?

rob@laptop:~/Desktop$ head stusers.txt
rob@laptop:~/Desktop$ ouse.com,passw0rd
like image 815
bobbyrne01 Avatar asked Nov 27 '12 14:11

bobbyrne01


People also ask

How do you add a string at the beginning of each line in Unix?

The ^ character is what instructs the sed command to add a character to the beginning of each line. Here's the syntax for adding a space to the beginning of each line using sed . Alternatively, use the -i option with the sed command to edit a file in place.

How do I add text to the beginning of a file in bash?

You cannot insert content at the beginning of a file. The only thing you can do is either replace existing content or append bytes after the current end of file.


5 Answers

Concerning your original error:

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

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. 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.)

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

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.)

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.

A variation on the while read... suggestion:

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):

while read -r line; do echo "kp" $line; done < stusers.txt 
like image 20
Nick Petersen Avatar answered Oct 07 '22 18:10

Nick Petersen


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

awk '{print "prefix" $0}' file 
like image 138
nullrevolution Avatar answered Oct 07 '22 18:10

nullrevolution


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

sed -e 's/^/prefix/' $file
like image 35
Jack Avatar answered Oct 07 '22 18:10

Jack


A Perl way to do it would be:

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.

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.

like image 23
Andy Lester Avatar answered Oct 07 '22 17:10

Andy Lester


Instead of the for loop, it is more appropriate to use 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
like image 34
William Pursell Avatar answered Oct 07 '22 17:10

William Pursell