Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for an 'Elegant' Solution 1

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I have a file which looks like this:

Code:
----------------------------------------------------------
|   Last Name     |  First Name | MI  | AcctNr  |  Status|
----------------------------------------------------------
| Duck            | Daffy       | D   | 012345  | 2      |
----------------------------------------------------------
| Flintstone      | Frederick   | F   | 223344  | 1      |
----------------------------------------------------------
(etc.)

which I need to turn into this:

Code:
Duck|Daffy|D|012345|2
Flintstone|Frederick|F|223344|1
(etc.)

I've written a short Perl script to do the change but I got to thinking that this could probably be done much faster and easier with sed and/or awk. Problem is, I'm just learning these tools and am getting nowhere.

Would appreciate ideas on doing this without using Perl so that I can learn as I go.

Thanks!

Tom

"My mind is like a steel whatchamacallit ...
 
Something like this?

Code:
awk '!/^$|\-/{gsub(/ +|^\| |\|$/,"");print}' filename

Regards,
Chuck
 
you could use the unix tr command to remove all blanks

-------------------------
The trouble with doing something right the first time is that nobody appreciates how difficult it was - Steven Wright
 
Chuck:

Works great other than it doesn't get rid of that first line (the header with "Last Name", etc. in it).

Thanks!

Tom

"My mind is like a steel whatchamacallit ...
 
This should take care of the header:

Code:
awk '!/Last|^$|\-/{gsub(/ +|^\| |\|$/,"");print}' filename

Regards,
Chuck
 
Chuck:

But I can do that with grep -v so this is 'the' solution!

Thanks again.

Best,

Tom

"My mind is like a steel whatchamacallit ...
 
Another way:
awk -F' *[|] *' 'NF>1 && NR>3{print $2"|"$3"|"$4"|"$5"|"$6}' filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Is that database query output?

If you're using MySQL, you can get the output in tab-separated format with no headings if you use the -B and --skip-column-names options in your "mysql" command. Then, if you want, you can easily convert the tabs to another character using tr.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top