Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Search this Thread
  #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: 64
Thanks: 0
Thanked 7 Times in 7 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..
  #3  
Old 03-13-2010
Registered User
 

Join Date: Aug 2009
Location: istanbul not constantinapole
Posts: 221
Thanks: 5
Thanked 0 Times in 0 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
Registered User
 

Join Date: Dec 2009
Posts: 705
Thanks: 6
Thanked 56 Times in 54 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
  #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
  #6  
Old 03-13-2010
durden_tyler's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 1,088
Thanks: 0
Thanked 43 Times in 41 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
  #7  
Old 03-14-2010
dennis.jacob's Avatar
dj - the student
 

Join Date: Feb 2007
Location: Bangalore/Cochin
Posts: 860
Thanks: 0
Thanked 5 Times in 4 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
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off


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 09:20 PM.