LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-16-2010, 04:38 PM   #1
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Rep: Reputation: 23
Need help on renaming a file


I have a file called unsubscribe with the following data:

user1
user2
user3
user4

I want to have it look like this for all users:

user1@domain.com;0;
user2@domain.com;0;
user3@domain.com;0;
user4@domain.com;0;

Any help would be greatly appreciated.
 
Old 07-16-2010, 04:50 PM   #2
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 44
You can use sed to append the common info after each user. It's a lot easier if each username has the exact same number of characters. If not Im sure there is a way in sed to append to after the last character in a line.


Look for some examples on how to use "sed".
 
Old 07-16-2010, 04:55 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by rhbegin View Post
I have a file called unsubscribe with the following data:

user1
user2
user3
user4

I want to have it look like this for all users:

user1@domain.com;0;
user2@domain.com;0;
user3@domain.com;0;
user4@domain.com;0;

Any help would be greatly appreciated.
The thread name is wrong - you are not trying to rename a file, you are trying to modify lines inside the file.

As an alternative to what's been already recommended read

man 1 paste
.
 
Old 07-16-2010, 05:46 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As Sergei already stated, this is not a problem about renaming but about how to concatenate strings by reading one of them from a file. Indeed the suggested sed solution is the most straightforward, since it lets you edit the file in place, e.g.
Code:
sed -i.bck 's/$/@domain.com;0;/' unsubscribe
but it mostly depend on the language/application of your choice, e.g. in pure bash:
Code:
while read line; do echo "${line}@domain.com;0;"; done < unsubscribe
using awk:
Code:
awk '$0=$0"@domain.com;0;"' unsubscribe
and so on...
 
Old 07-18-2010, 08:07 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Appends a string to every non-blank line:

Code:
awk '/.+/ { print $0 "@domain.com;0;" }'
 
Old 07-18-2010, 06:00 PM   #6
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Original Poster
Rep: Reputation: 23
I am a beginner in bash, thank you so much for the prompt replies.

Would this be the correct solution then:

awk '/.+/ { print $0 "@domain.com;0;" }' filename
 
Old 07-18-2010, 07:02 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I guess so.
 
Old 07-18-2010, 07:38 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'NF{$0=$0"@domain.com;0;"}1' file
 
Old 07-18-2010, 08:42 PM   #9
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Original Poster
Rep: Reputation: 23
That did the trick!


Thank you!!
 
  


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: renaming file extension based on actual file type alekone Linux - General 9 12-28-2008 09:12 AM
file renaming computer_tom Linux - General 3 02-18-2008 12:27 PM
File renaming amushtaq Linux - Software 2 01-13-2006 05:40 AM
renaming a file?? U-Toast Linux - Newbie 2 08-29-2004 01:07 AM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 06:57 AM

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

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