LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-05-2012, 11:53 AM   #1
gluposti
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
sed - replacing random numbers followed with space and more random chars


I have big file with pattern going something like this

Code:
Name   CompanyName   ZIP Town   RandomNumbers
The thing is, between ZIP and Town is just a space and i need to replace it with tab. I cant just replace all spaces with tabs because there are spaces also in name and in company name.
I know it should be something like replace
Code:
's/[:digit:][:blank:][:alpha:]/
, but not sure how to replace it with that same digits and characters.
 
Old 05-05-2012, 11:59 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

ZIP code for which country and what is its layout? Or does this need to work for all ZIP codes world wide?
 
Old 05-05-2012, 12:05 PM   #3
gluposti
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Worldwide. Actually, ZIP code is just 5 random numbers. Tab before it, space after, which i need to replace with tab.
 
Old 05-05-2012, 12:17 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by gluposti View Post
Worldwide. Actually, ZIP code is just 5 random numbers.
I doubt that, a proper ZIP code for the Netherlands would be 1234 AB.

If 5 random numbers are used:
Code:
sed 's/\([0-9]\{5\}\) /\1      /' infile
That is a tab after the \1.

The above changes 5 numbers and a space by the same 5 numbers and a tab.

Hope this helps.
 
Old 05-05-2012, 12:27 PM   #5
gluposti
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Yup, that works, thanks!
 
Old 05-05-2012, 12:32 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
Old 05-05-2012, 01:07 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Well, it looks like I'm late to the party but...

Are the fields tab-delimited, or whitespace-delimited in some other way that's different from the spaces inside the fields? Strings of 2 or more spaces, for example?

If there's some way to clearly define a delimiter, awk would probably be a better tool to use. If the delimiters are tabs, for example:

Code:
awk 'BEGIN{ FS=OFS="\t" } { gsub(/[ ]/,"\t",$3) ; print }' file
The FS setting splits the file into fields based on tabs, then the gsub function replaces all spaces inside field3 with tabs, then the resulting line is printed, with OFS delimiters (defined to be the same as FS) added back between fields.

If you have some other clear pattern for fields, you should be able to add it to the FS instead. For 2+ spaces, for example, you could replace the BEGIN section above with this:

Code:
BEGIN{ FS="[ ][ ]+" ; OFS="  " }
( Unfortunately OFS can only contain a literal string value, so you have to set it separately when FS is a complex regex. AFAIK, you can't preserve the original field separators in awk, at least not without writing up a custom line parser ).

Otherwise, if we actually need to actually match the exact pattern of content, a sed solution like the one above would probably be a better choice.

Last edited by David the H.; 05-05-2012 at 01:09 PM. Reason: minor fixes
 
Old 05-07-2012, 08:36 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Quote:
I doubt that, a proper ZIP code for the Netherlands would be 1234 AB.
Indeed, UK would be eg 'PO41 0XF' ie a mix of nums/letters with a space in between 2 fields ...
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Random numbers between -5.0 to +5.0 smp Linux - Newbie 12 08-17-2010 07:57 PM
Can't get virtualbox to work - random chars error. Red Squirrel Linux - Software 1 01-25-2009 04:49 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 02:42 PM
random numbers deveraux83 Programming 4 05-17-2005 03:26 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 01:12 PM


All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration