How to delimit a flat file with records with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delimit a flat file with records with SED
# 1  
Old 10-28-2008
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  
Old 10-28-2008
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  
Old 10-28-2008
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  
Old 10-28-2008
still problem with the code

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

Thanks in advance
# 5  
Old 10-28-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delimit the fields of a input file which has special characters?

Hi All, I am a newbie to Shell scripting. I have a requirement to Delimit the file fields of a Input file having special characters and spaces with ";". Input File ---------------------------------- Server Port ---------------------------------- Local ... (5 Replies)
Discussion started by: Suganbabu
5 Replies

2. Shell Programming and Scripting

Delimit file based on character length using awk

Hi, I need help with one problem, I came across recently. I have one input file which I need to delimit based on character length. $ cat Input.txt 12345sda231453 asd760kjol62569 sdasw4g76gdf57 And, There is one comma separated file which mentions "start of the field" and "length... (6 Replies)
Discussion started by: Prathmesh
6 Replies

3. Shell Programming and Scripting

sed flat file manipulation

Hello, I have a large flat file where i need to change data in columns 131-133 based on what is in columns 172-173. I am not sure if I need to read the file line by line and make the change or if I can do this in a single statement. thank you (3 Replies)
Discussion started by: gblmin
3 Replies

4. Shell Programming and Scripting

How to remove space and delimit a csv file?

Hi All , I am facing a small challenge i need unix command to remove space as well as replace "|" to "," in a csv file . original file : A | B | c | D E | F | G | H I | J | K | L P | M | N | O Expected o/p: A,B,c,D E,F,G,H I,J,K,L P,M,N,O (4 Replies)
Discussion started by: Sweety532
4 Replies

5. Shell Programming and Scripting

Remove somewhat Duplicate records from a flat file

I have a flat file that contains records similar to the following two lines; 1984/11/08 7 700000 123456789 2 1984/11/08 1941/05/19 7 700000 123456789 2 The 123456789 2 represents an account number, this is how I identify the duplicate record. The ### signs represent... (4 Replies)
Discussion started by: jolney
4 Replies

6. Shell Programming and Scripting

SED | Awk flat file one liner

sed awk one liner (2 Replies)
Discussion started by: jap2614
2 Replies

7. Shell Programming and Scripting

How do I load records from table to a flat file!

Hi All, I need to load records from oracle table XYZ to a flat file say ABC.dat. could any one tell me how do i do this in UNXI, Regards Ann (1 Reply)
Discussion started by: Haque123
1 Replies

8. Shell Programming and Scripting

Sorting the records in the Flat file

Hi all, I am using this command "sort -d -u -k1 IMSTEST.74E -o tmp.txt" to the records in the flat. Can any tell me how to sort the file except first line in the file For ex: i/p First line: DXYZ Second line : jumy third : cmhk fourth : andy Output should... (5 Replies)
Discussion started by: sudhir_barker
5 Replies

9. UNIX for Dummies Questions & Answers

Problems with sed and flat file variables

Hello All, It has been a loooooooooooong time since I had last used sed but decided to use it for a simple task I have . My goal is to use sed to read variables from a flat file then use those same variables in order to make some subsitutions. However what I am finding is that when the... (1 Reply)
Discussion started by: icalderus
1 Replies

10. Shell Programming and Scripting

Inserting records from flat file to db table

I have 20000 numbers present in a file in each line like 25663, 65465, 74579, 56446, .. .. I have created a table in db with single number column in it. create table testhari (no number(9)); I want to insert all these numbers into that table. how can i do it? can anybody please... (4 Replies)
Discussion started by: Hara
4 Replies
Login or Register to Ask a Question