LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Transfer lines from one file to another (https://www.linuxquestions.org/questions/programming-9/transfer-lines-from-one-file-to-another-475867/)

chloraldo 08-21-2006 07:41 AM

Transfer lines from one file to another
 
Hello

I have one file that has one line in it. I have to transfer that line to the end of another file but add some numbers before that line and some number after it. So I do something like this:

Code:

whatever=`cat otherfile`

printf 2343 > newfile
printf $whatever >> newfile
echo 121 >> newfile

That works fine when otherfile has just one line. e.g:

22439999121 (if otherfile contains 9999)

But now the problem:

otherfile sometimes has two lines, e.g.
9999
8888

and what I want in newfile is this:
22439999121
22438888121

Any ideas how I can do that for more than one line?

homey 08-21-2006 08:14 AM

Using cat might look like this
Code:

#!/bin/bash

cat file.txt | while read line ; do
  echo "2343"$line"121" >> newfile.txt
done

A sed one liner might look like this ...
Code:

sed -e 's/^.*$/2343&121/' file.txt > newfile.txt

chloraldo 08-21-2006 08:58 AM

Quote:

Originally Posted by homey
Using cat might look like this
Code:

sed -e 's/^.*$/2343&121/' file.txt > newfile.txt

Thanks, great!

Still one question. If the 2343 part is a variable, how can I put the variable into the code?

This doesn't work:
Code:

file1=2343
sed -e 's/^.*$/$file1&121/' file.txt > newfile.txt


homey 08-21-2006 09:08 AM

Put the variable in single quotes
Code:

sed -e 's/^.*$/'$mudd'&121/' file.txt > newfile.txt


All times are GMT -5. The time now is 09:32 PM.