Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-13-2010
Registered User
 

Join Date: Mar 2009
Location: Indianapolis
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Remove SPACES between PIPE delimited file

This is my input file with extra information in the HEADER and leading & trailing SPACES between PIPE delimiter.
02/04/2010 Dynamic List Display 1
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
| FIST_NAME|LAST_NAME|ADDRESS |
--------------------------------------------------------------------------------------
| Rajesh |Verma |10/27/2004|
| James |David Jr |10/08/2009|
--------------------------------------------------------------------------------------

How to make output file like this, please help with UNIX script

FIST_NAME|LAST_NAME|ADDRESS|
Rajesh|Verma|164 N Tutor Ln, Chicago-IL|
James|David Jr|529 Carlton Arms Dr, Chicago-IL|

Thanks
srimitta
Sponsored Links
    #2  
Old 03-13-2010
Registered User
 

Join Date: Mar 2010
Posts: 141
Thanks: 0
Thanked 19 Times in 19 Posts
it's funny

02/04/2010 Dynamic List Display 1
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
| FIST_NAME|LAST_NAME|ADDRESS |
--------------------------------------------------------------------------------------
| Rajesh |Verma |10/27/2004|
| James |David Jr |10/08/2009|
--------------------------------------------------------------------------------------

How to make output file like this, please help with UNIX script

FIST_NAME|LAST_NAME|ADDRESS|
Rajesh|Verma|164 N Tutor Ln, Chicago-IL|
James|David Jr|529 Carlton Arms Dr, Chicago-IL|

What's from the address???

Last edited by john1212; 03-13-2010 at 04:13 PM..
Sponsored Links
    #3  
Old 03-13-2010
Registered User
 

Join Date: Aug 2009
Location: istanbul not constantinapole
Posts: 269
Thanks: 15
Thanked 10 Times in 10 Posts

Code:
awk -F\| '{for(i=1;i<=NF;i++) gsub("^[ \t]*|[ \t]*$","",$i)}1' OFS=\| FILE


Last edited by EAGL€; 03-13-2010 at 05:57 PM.. Reason: changed delimeter from "#" to "|"
    #4  
Old 03-13-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
Hi, srimitta:


Code:
$ cat data
02/04/2010 Dynamic List Display 1
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
| FIST_NAME|LAST_NAME|ADDRESS |
--------------------------------------------------------------------------------------
| Rajesh |Verma |10/27/2004|
| James |David Jr |10/08/2009|
--------------------------------------------------------------------------------------

$ sed -n '/^| */{s///;s/ *| */|/g;p;}' data
FIST_NAME|LAST_NAME|ADDRESS|
Rajesh|Verma|10/27/2004|
James|David Jr|10/08/2009|

Regards,
Alister
Sponsored Links
    #5  
Old 03-13-2010
Registered User
 

Join Date: Mar 2009
Location: Indianapolis
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
john1212, Address was copy / paste mistake.


Thanks Alister worked like charm.
Quote:
sed -n '/^| */{s///;s/ *| */|/g;p;}' data

Regards
srimitta
Sponsored Links
    #6  
Old 03-13-2010
durden_tyler's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 1,684
Thanks: 4
Thanked 209 Times in 189 Posts
Using Perl:


Code:
$ 
$ cat f0
02/04/2010 Dynamic List Display 1
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
| FIST_NAME|LAST_NAME|ADDRESS |
--------------------------------------------------------------------------------------
| Rajesh |Verma |10/27/2004|
| James |David Jr |10/08/2009|
--------------------------------------------------------------------------------------
$ 
$ perl -lne 's/\s*\|\s*/\|/g && print' f0
|FIST_NAME|LAST_NAME|ADDRESS|
|Rajesh|Verma|10/27/2004|
|James|David Jr|10/08/2009|
$ 
$

tyler_durden
Sponsored Links
    #7  
Old 03-14-2010
dennis.jacob's Avatar
Registered User
 

Join Date: Feb 2007
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 10 Times in 9 Posts
Quote:
Originally Posted by EAGL€ View Post
Code:
awk -F\| '{for(i=1;i<=NF;i++) gsub("^[ \t]*|[ \t]*$","",$i)}1' OFS=\| FILE


Another straight awk solution.


Code:
awk -F"|" '/^\|/ { OFS="\|"; gsub(/ /,"",$0); print $2,$3,$4,$5; }' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete last value from pipe delimited file relentl3ss UNIX for Dummies Questions & Answers 3 02-17-2010 05:27 AM
convert a pipe delimited file to a':" delimited file priyanka3006 Shell Programming and Scripting 6 05-26-2009 10:53 AM
Extracting from pipe delimited file. leepan2008 UNIX for Dummies Questions & Answers 1 02-17-2009 02:55 AM
How to generate a pipe ( | ) delimited file? anushree.a Shell Programming and Scripting 5 10-15-2008 02:35 AM
How to split pipe delimited file njgirl Shell Programming and Scripting 4 06-18-2008 05:15 PM



All times are GMT -4. The time now is 03:44 AM.