- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk - removing whitespace before and after text
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2003 01:03 AM
тАО01-15-2003 01:03 AM
bit of a dilema with this one.
I have a file that has data similar to the following :
20000003985|| | 32| |KINGS 20000003988|||41||SADLER
20000003991||FLAT 1| 21| |JACKSONS
20000003993|| |DOVE COTTAGE | |HOCKLEY
20000004152|| | 115| |AMBLESIDE
20000004157|||51||FARADAY 20000004158|||20||MILLENNIUM
20000004215|||HAMPTON ||LEICESTERSHIRE
20000004216|||46||BURGESS
20000004218|||65||CROMFORD WAY
20000004220|| | 8| |DUCKWORTH
20000004257| CHEZ SOI | | 27| |HIGH
What I need to do is trim the whitespace(both ends) off all the pipe delimtted fields. For example the row :
20000004257| CHEZ SOI | | 27| |HIGH
should idealy be :
20000004257|CHEZ SOI||27||HIGH
The fields containing the whitespaces are arbitrary (so I would have to check all fields).
I was attempting to do this using an awk statement, but am too inexperienced at awk to figure it out. I am confident I could do it in java, but prefereably via a UNIX script.
eternally grateful
John
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2003 01:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2003 01:08 AM
тАО01-15-2003 01:08 AM
Re: awk - removing whitespace before and after text
try with:
cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2003 01:22 AM
тАО01-15-2003 01:22 AM
Re: awk - removing whitespace before and after text
cat /tmp/file |sed 's/| /|/g' |sed 's/ |/|/g'.
If not, perform this several times until 'grep " |" /tmp/file' and 'grep "| " /tmp/file' returns zero lines.
Cheers, Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2003 01:23 AM
тАО01-15-2003 01:23 AM
Re: awk - removing whitespace before and after text
# perl -pe's/\s*\|\s*/\|/g' infile
to trim start and end of line too
# perl -pe's/\s+$//;s/^\s+//;s/\s*\|\s*/\|/g' infile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2003 02:33 AM
тАО01-15-2003 02:33 AM
Re: awk - removing whitespace before and after text
sed -e 's/ *| */|/g' filename
Rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2003 07:11 AM
тАО01-16-2003 07:11 AM
Re: awk - removing whitespace before and after text
The way to match zero or more whitespace characters in perl is: \s*
The way to match zero or more whitespace characters in sed (and grep, vi, ex, more, etc) is: [[:space:]]*
So if you want to strip whitespace before and after the pipes and before and after the text on the line, then you'd use this:
cat file |sed -e 's/^[[:space:]]*//' |sed -e 's/[[:space:]]*$//' |sed -e 's/[[:space:]]*|[[:space:]]*/|/g'
As for awk... well, can't help ya there. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2003 07:41 AM
тАО01-16-2003 07:41 AM
Re: awk - removing whitespace before and after text
Do you dream about writing perl scripts when you sleep? hehe
Chuck J