LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-10-2010, 02:21 PM   #1
teresevo1
LQ Newbie
 
Registered: Apr 2010
Posts: 11

Rep: Reputation: 0
adding number and string to each line in a data file


I have a very large data file, with 3 numbers in each line, ex. 3 4 5 ; 6 7 8 ....

I want to add two numbers (0 and 1) at the end of every line, and a string at the beginning of the line. Does anyone know a efficient way to do so?

Last edited by teresevo1; 04-10-2010 at 02:43 PM.
 
Old 04-10-2010, 02:36 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Please provide more information on what these numbers are for, and what determines the forth number and the string. That may determine the best tool to use. For regular formatted records that may require computation on these numbers, awk my be the best tool to use, but more details are needed to know for sure. You may need a general purpose language such as perl or python if you need access to one of their libraries to carry out the task.
 
Old 04-10-2010, 02:48 PM   #3
teresevo1
LQ Newbie
 
Registered: Apr 2010
Posts: 11

Original Poster
Rep: Reputation: 0
the number at the end of the lines are the same for every line in the file, representing the name of the dataset.
 
Old 04-10-2010, 02:58 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by teresevo1 View Post
I have a very large data file, with 3 numbers in each line, ex. 3 4 5 ; 6 7 8 ....

I want to add two numbers (0 and 1) at the end of every line, and a string at the beginning of the line. Does anyone know a efficient way to do so?
An efficient way would be to write a script. Or even a "C" program - the task is very simple.

Do you know Perl or "C" ?
 
Old 04-10-2010, 03:03 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
To add something to the beginning or end of each line in a file, I would use SED.

Examples:
Code:
Adding hello to the beginning of every line:
sed 's/^/hello /' filename > newfilename

Adding "23" to the end of every line:
sed 's/$/ 23/' filename > newfilename
Very good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 04-10-2010, 04:45 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Since you mentioned doing both:
Code:
sed 's/^.*$/hello & 23/'  filename > newfilename
 
Old 04-10-2010, 10:12 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or just a simple awk:

Code:
 awk '{print "string "$0" 0 1"}' file
 
Old 04-11-2010, 12:03 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP, if you need to add things to the front and back of strings, just print them out...like what grail posted. Or this
Code:
awk '{$0="string "$0" 0 1"}1' file
 
  


Reply



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
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Look for a string on a file and get its line number horacioemilio Programming 15 01-08-2008 08:32 PM

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

All times are GMT -5. The time now is 09:16 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