LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2006, 07:41 AM   #1
chloraldo
LQ Newbie
 
Registered: Aug 2006
Posts: 9

Rep: Reputation: 0
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?
 
Old 08-21-2006, 08:14 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 08-21-2006, 08:58 AM   #3
chloraldo
LQ Newbie
 
Registered: Aug 2006
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 08-21-2006, 09:08 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Put the variable in single quotes
Code:
sed -e 's/^.*$/'$mudd'&121/' file.txt > newfile.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing new lines (\n) from a file bkeeper Linux - Software 4 12-15-2005 02:13 AM
How can I sort the lines in a file? windhair Linux - Software 2 11-17-2005 08:37 AM
erase all the lines of a file Prasun1 Linux - General 9 11-06-2005 10:53 AM
How to remove lines from a file doza Linux - General 2 04-27-2005 11:59 AM
delete some lines from a file freelinuxcpp Linux - Software 4 01-17-2004 10:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration