Adding to the front of a line

Matthew Flaschen matthew.flaschen at gatech.edu
Tue Jan 20 04:16:49 UTC 2009


Ray Parrish wrote:
> Hello again,
> 
> That *almost* worked perfectly, with the exception that it is adding a 
> blank line between each line in the output file which is undesirable. 8-)

Okay, I figured this out.  You must have a CRLF file, and I left a space
 after $site by mistake which screws things up for that.  Use

while read site; do
echo "127.0.0.1 $site"
done < sites.txt > output_file.txt

Or, try this alternative method:

sed "s/.*/127.0.0.1 &/" sites.txt>output_file.txt

Note this also screws up on CRLF if there's a space after the &.  It's
equivalent to the above, just terser.

> #!/usr/bin/env bash
> # Add a text string to the front of each line in a file.
> # Command line parameters $1 - file to read in $2 - string to add to 
> front of line $3 - output filename
> #
> if [ $1 == "" ] || [ $2 == "" ] || [ $3 == "" ]

I think you mean:

if [[ $1 == "" ]] || [[ $2 == "" ]] || [[ $3 == "" ]]

The first will work as expected... unless of course any of $1, $2, or $3
is blank in which case you'll get:

bash: [: ==: unary operator expected

or similar (in other shells).

The second will work either way. :)

Matt Flaschen




More information about the ubuntu-users mailing list