The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare XML/flat file with UNIX file system structure shafi2all High Level Programming 6 08-15-2008 02:15 AM
Use records from one file to delete records in another file kenneth.mcbride UNIX for Dummies Questions & Answers 5 06-18-2008 02:36 PM
Flat file Krishnaramjis Shell Programming and Scripting 9 05-08-2008 10:28 PM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
Inserting records from flat file to db table Hara Shell Programming and Scripting 4 12-27-2006 03:35 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-27-2008
RogCor RogCor is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
How to delimit a flat file with records with SED

Hi gurus, hoping someone can help with a sed line that can do the following...

I have a flat file with about 1000 records, but in order to import into openoffice spreadsheet, I need to create a delimited file. I'd like to do 2 things with the SED command:
1- add a pipe character "|" at the end of each line
2- (the above will create a double pipe for every two blank links, such as ||). Replace the || double-pipe with carriage returns so in the end I have a delimited file. Im not rigid on how to perform step 2, so any recommendations are appreciated. See the file format below for a better view.


The file format is like this:

Person1
Address
Value1
License Status blah blah blah

<2 carriage returns>
Person2
Address
Value1
License status.. blah...

<2 Carriage returns>
etc...


After running the text file through a SED command, i'd like the file to look like this:

Person1|Address|Value1|License Status blah blah blah
Person2|Address|Value1|License status.. blah...
Person3|etc...


Any help is greatly appreciated! Thanks in advance!
  #2 (permalink)  
Old 10-28-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 243
Hi, I'm not sure if it's OK with You but I would use a combination of tr and sed, because it feels natural, since You expressed the problem in two steps. First let tr replace each newline with | and then let sed replace double || with a newline, like this:
Quote:
tr "\n" "|" < data.txt |sed 's/||/\n/g'
anyway I think You would still need two "passes" if using sed for both tasks.

/Lakris
  #3 (permalink)  
Old 10-28-2008
FourBrane FourBrane is offline
Registered User
  
 

Join Date: May 2008
Location: Baton Rouge, LA
Posts: 4
Agree that tr plus sed is best

Only addition I'd make is that sed's substitute command will need to look for three pipes in a row as the two intervening newlines actually represent the second and third newlines in a row.

So, you'd want
tr "\n" "|" < input.txt | sed 's/|||/\n/g'
  #4 (permalink)  
Old 10-28-2008
u4xmnsk u4xmnsk is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 6
still problem with the code

The above code not generating any output.Could you please suggest me ?

Thanks in advance
  #5 (permalink)  
Old 10-28-2008
RogCor RogCor is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
It Worked!

The code snippets above worked flawlessly! Thanks for the tips. Cant tell you how much time it saved me! I love my HP notebook running openSUSE 11!

Cheers,
RogCor
Closed Thread

Bookmarks

Tags
delimiter, linux, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0