[ale] swap 2 fields (in a tabbed file)?

Danny Cox danscox at mindspring.com
Wed Jan 29 12:55:18 EST 2003


CB,

On Wed, 2003-01-29 at 12:34, Christopher Bergeron wrote:
> Does anyone know how I can swap 2 fields in a tab delimited file
> containing 2 fields?
> 
> Here's an example of my data:
> ~ <TAB>WOODSTOCK OUTLET<CR>
> AF100<TAB>METRO AUDIO DYNAMI<CR>
> 
> What I'd like to do is swap the 2 fields (or do a `sort` based on the
> second field!) such that I end up with this:
> WOODSTOCK OUTLET<TAB> <CR>
> METRO AUDIO DYNAMICS<TAB>AF100<CR>
> 
> Does anyone know of a way I can accomplish this?  I tried using cut
> - -f2,1 -d"\t" but no adeu...

	Is that "<CR>" a '\r', or is it a '\r\n'?  If it really is just a \r,
most UNIX/Linux tools will treat it as whitespace.  If all you really
want to do is sort, see "sort -t'\t' +1 file >sorted", but replace the
\t with an literal <TAB> character.

	OTOH, you may want to explore the sed capital commands (DGHP) to first
get the middle <CR> deleted, much with the line (almost all filters are
line based), and then re-insert the <CR>.

	Of course, Perl could do this in once swipe.

	while (<>) {
		chomp;		# remove the newline
		$line1 = $_;
		$_ = <>;	# read the 2nd line
		chomp;		# remove the newline
		print "$_\t$line1\n";	# print'em backwards
	}

	There's probably simpler ways to do that....

-- 
kernel, n.: A part of an operating system that preserves the
medieval traditions of sorcery and black art.

Danny

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list