LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 08-21-2007, 08:01 AM   #1
rduncan10
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
Delete Multiple Column Headers


Hi,

Our ERP system on SUSE can print reports to a printer or an ASCII file. I am writing a script to strip away all the page header/footer content from the ASCII file, leaving just the data, so that it can be imported into another program.

I've got most of this stuff cleaned out, but I'm left with the column headers, which are repeated at the top of each page.

I am tryting to find out if there is some sed command for deleting all but the first occurence of a line.

I can't use uniq because there might be duplicate lines within the data that I don't want to delete.

I can do it like this:

Code:
hdr=$(head -1 file)
echo $hdr > newfile
cat file | sed '/$hdr/d' >> newfile
or 
cat file | grep -v $hdr >> newfile
This works fine, but I was wondering if there might be some sed trick to this that might work better.


Thanks.
 
Old 08-21-2007, 08:40 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
show how your file looks like...
 
Old 08-21-2007, 01:02 PM   #3
rduncan10
LQ Newbie
 
Registered: May 2007
Posts: 8

Original Poster
Rep: Reputation: 0
It looks something like this

ITEM | DESC | CUSTOMER | PRICE
123 | Grummit | C1123 | 2.20
456 | Grammit | C123 | 15.20
...
ITEM | DESC | CUSTOMER | PRICE
2929 | Gear | C1123 | 939.39


Our ERP system uses the pipe characther to delimit fields. The column header ("ITEM | DESC" etc) is repeated at the top of each page. I want to turn it into this:

ITEM | DESC | CUSTOMER | PRICE
123 | Grummit | C1123 | 2.20
456 | Grammit | C123 | 15.20
2929 | Gear | C1123 | 939.39


This way the data can be imported into another program without having to do much extra work.

Right now the script works like this:

Code:
#!/bin/sh                            
                                     
cat $1 | grep "|" > $1.tmp             # get rid of all lines that don't have | character
                                       # (all header/and footer lines)
hdr=$(head -1 $1.tmp)                  # store first line (column header)
echo $hdr > $1                         # add column header to new file.
cat $1.tmp | grep -v "$hdr" >> $1      # Add remaining lines to new file, except those matching
                                       # header
rm $1.tmp
This works fine, but I just thought there might be some sed technique to do this.

Thanks.
 
Old 08-21-2007, 01:24 PM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 34
Quote:
Originally Posted by rduncan10 View Post
It looks something like this

ITEM | DESC | CUSTOMER | PRICE
123 | Grummit | C1123 | 2.20
456 | Grammit | C123 | 15.20
...
ITEM | DESC | CUSTOMER | PRICE
2929 | Gear | C1123 | 939.39


Our ERP system uses the pipe characther to delimit fields. The column header ("ITEM | DESC" etc) is repeated at the top of each page. I want to turn it into this:

ITEM | DESC | CUSTOMER | PRICE
123 | Grummit | C1123 | 2.20
456 | Grammit | C123 | 15.20
2929 | Gear | C1123 | 939.39

[...]
Code:
sed '2,${/^I/d;}' infile
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gnumeric's worksheet doesn't display row and column headers correctly frazum Linux - Software 4 07-09-2007 11:53 AM
vi editor - how to delete a character in each row of a column? flipwhy Linux - Newbie 6 01-22-2007 02:12 PM
how to delete different column in a file? Tuku Programming 3 01-04-2007 09:53 AM
how to delete different column in a file? Tuku Linux - Newbie 2 01-04-2007 12:47 AM
How to delete a column in MySQL ? indian Linux - General 2 10-15-2004 01:02 PM


All times are GMT -5. The time now is 04:41 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
Open Source Consulting | Domain Registration