LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 10: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, 10:59 AM   #2
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 11:05 AM   #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, 11:17 AM   #4
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 11:27 AM   #5
gluposti
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Yup, that works, thanks!
 
Old 05-05-2012, 11:32 AM   #6
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
Old 05-05-2012, 12:07 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 12:09 PM. Reason: minor fixes
 
Old 05-07-2012, 07:36 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430

Rep: Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787
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 06:57 PM
Can't get virtualbox to work - random chars error. Red Squirrel Linux - Software 1 01-25-2009 03:49 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
random numbers deveraux83 Programming 4 05-17-2005 02:26 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:51 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
Open Source Consulting | Domain Registration