LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-28-2008, 05:42 PM   #1
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Rep: Reputation: 15
replacement with sed


Hello. I want to append a string with value "NULL" only to the end of the registers which end with semicolon:

The command Iīm using is:

Code:
 sed 's/;$/;NULL/g' filename
It doesnīt work, I donīt know what am I doing wrong.
Any ideas of why itīs not working? Thanks for the help.

Last edited by DeepSeaNautilus; 09-28-2008 at 11:58 PM.
 
Old 09-28-2008, 05:52 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
If you mean "lines" when you say "registers", then it works fine here. Note that the "g" has no function because you are matching only once--ie at the end of the line.

Another way of doing the same thing:
sed '/;$/s/$/NULL/' filename

If you have data in multiple columns, then you may need something like AWK.

Please post an example of the data, and what is not working correctly.
 
Old 09-28-2008, 11:52 PM   #3
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
If you mean "lines" when you say "registers", then it works fine here. Note that the "g" has no function because you are matching only once--ie at the end of the line.

Another way of doing the same thing:
sed '/;$/s/$/NULL/' filename

If you have data in multiple columns, then you may need something like AWK.


Please post an example of the data, and what is not working correctly.
Thanks. I am making a script for parsing a raw file text with database registers into a file with INSERT INTO sql instructions.

The plain text document may have something like this:
id;name;last name; address; telephone; cellphone; email
Some of the atributes may have null values, like this:
id;name;;address;;cellphone;

In this case I need to fill with the word NULL the null fields so I can later create an INSERT INTO sql instruction later on (using awk), such like this:

id;name;NULL;address;NULL;cellphone;NULL

I have been able to insert every NULL string except for the one at the end.

The problem is that when I use both commands I am still getting
1;... ; instead of 1; .... ;NULL

Last edited by DeepSeaNautilus; 09-29-2008 at 12:04 AM.
 
Old 09-29-2008, 06:35 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
When you say "both commands", I'm not sure what you are referring to.

Here's one crude way to do it:
sed -e 's/;;/;NULL;/g' -e 's/;$/;NULL/' filename > newfilename

Really good SED tutorial (and much more) here:
http://www.grymoire.com/Unix/Sed.html

Bash documents here:
http://tldp.org
 
Old 09-29-2008, 08:34 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
If the file was created under Windows/DOS it could have line endings CRLF, i.e. \r\n.
Unix files use only LF, i.e. \n
If this is the cause of the problem and you have GNU sed try:-
Code:
sed 's/;\r$/;NULL\r/' filename
 
Old 09-30-2008, 11:50 PM   #6
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Problem solved. Thanks

Quote:
Originally Posted by Kenhelm View Post
If the file was created under Windows/DOS it could have line endings CRLF, i.e. \r\n.
Unix files use only LF, i.e. \n
If this is the cause of the problem and you have GNU sed try:-
Code:
sed 's/;\r$/;NULL\r/' filename
I just realized that the last character of the line was not the semicolon, even though it seemed to be, that is why the suggestions given above did not work. As you said, this file was created in windows and there were several spaces between the semicolon and the end of line.
I removed those spaces with this:
Code:
sed -e 's/ *$//g' file > tempFile
And then I put the NULL string at the end of every line ended with semicolon with this:
Code:
 sed -e  's/;$/;NULL/' tempFile > file
 
Old 10-01-2008, 06:48 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
You can also use [[:space:]] to remove all whitespace---it works on both spaces and tabs. I have not tried it for your particular example.
 
  


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
Replacement with sed or awk DeepSeaNautilus Programming 4 08-05-2008 11:17 AM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 11:53 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
How to use *sed* to do this "REPLACEMENT"? yuzuohong Linux - General 3 08-20-2002 06:18 PM


All times are GMT -5. The time now is 06:07 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration