Solved

Changing Field Separaton in NAWK

Posted on 2004-10-06
5
579 Views
Last Modified: 2006-11-17
Hi,

I have a tab delimited file called 'data.5'.  I need to change it from tab delimited to ':' delimited.

BEGIN {OFS=":"}

nawk -F ":" { print } data.5 > data5.new

Where am I going wrong?

 
0
Comment
Question by:elwayisgod
  • 4
5 Comments
 

Author Comment

by:elwayisgod
ID: 12245617
I'm getting closer,  however this just puts the colons on the end.  Can I even do this with NAWK?

nawk -F: '{ print $1"\:",$2"\:",$3"\:",$4 }' data.5 > data5.new

broncos:/Users/stoteve/regis/week5 46 % more data5.new
Dunny      John      303-555-1212      Utah: : :
Knox      Bill      402-498-7890      Nebraska: : :
Jones      Mary      908-456-1243      Montana: : :
Smith      Barb      319-456-2390      Utah: : :
Utah      Hank      301-494-1009      Wyoming: : :
0
 
LVL 20

Expert Comment

by:tfewster
ID: 12246200
nawk -F "\t" { print } data.5 > data5.new
(-F is the file seperator char used in the _input_ file)

The
BEGIN {OFS=":"}
syntax is fine
0
 
LVL 20

Expert Comment

by:tfewster
ID: 12246222
As for
nawk -F: '{ print $1"\:",$2"\:",$3"\:",$4 }' data.5 > data5.new

Two things:
It's looking for an IFS of ":"; And doesn't find it, so $1 is the whole line, "Dunny     John     303-555-1212     Utah"; $2-4 are undefined.

Plus, if you seperate the output fields with "," nawk will substitute the OFS anyway; So you would get
Dunny::John::303-555-1212::Utah
0
 
LVL 20

Expert Comment

by:tfewster
ID: 12246256
Tested with Gnu awk, but I'm confident it will work with nawk ;-)

awk -F"\t" 'BEGIN {OFS=":"} {print $1,$2,$3}' data.5 > data5.new
0
 
LVL 20

Accepted Solution

by:
tfewster earned 50 total points
ID: 12246269
Oops, missed $4
awk -F"\t" 'BEGIN {OFS=":"} {print $1,$2,$3,$4}' data.5 > data5.new
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
g++ pthread_init failure on AIX 10 70
Best way to handle awk return value 6 66
IBM flash storage 840 15 58
Install MySQL 5.6 and PHP on Centos Linux 6 99
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now