Solved

Awk

Posted on 2000-02-17
4
991 Views
Last Modified: 2008-02-26
I have a long string of text, for example:

A12345678917/02/00Mr Smith

I need to create an output file with each value on a separate line.  The values are either text, numeric or dates etc and are fixed widths.

I have tried using awk and substr to select the first 10 characters and output on a line, followed by linefeed, then the next 8, then line feed, then next 15 and line feed etc.

Can anyone provide me with an awk script to do this - or an alternative program I can use?

By the way im using GNUs awk on win95 platform.

thanks and regards, kevin
0
Comment
Question by:kevinlent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
tel2 earned 50 total points
ID: 2530801
kevinlent,

If you've got access to "cut" (comes standard with UNIX), then try this:

  cut -c-8   <infile  >outfile
  cut -c9-16 <infile >>outfile
  cut -c17-  <infile >>outfile

Assuming the data is in a file called infile, and you want the output in outfile.  If you want the output on the screen, omit the ">outfile" and ">>outfile"s.

Let me know how you get on.

What other UNIX utilities have you got in this Win95?
0
 

Author Comment

by:kevinlent
ID: 2530896
Now using right tools for the job, never used cut before, this works a treat!

Thanks very much

You asked what other unix utils I was using: I have a copy of unixdos (downloaded from shareware.com) which includes about 60 of the best unix utils ported for 32bit windows.  I also have GNUs grep/egrep/fgrep, sed, diff and awk utils.

thanks again, kevin
0
 

Author Comment

by:kevinlent
ID: 2530911
Tel2,

Unfortunately cut does a column at a time, not a record at a time.

So cut doesnt do what I had hoped, any more ideas :(
0
 
LVL 12

Expert Comment

by:tel2
ID: 2533471
Sorry Kelvin,

I didn't know your string could be longer than 1 line.  If you have the standard "sed" utility, then you could do this:

Here's a sample infile:
  A12345678917/02/00Mr Smith
  B98765432128/02/01Mrs Smith

And this command:
  cat infile | sed "s/^\(..........\)\(........\)\(.*\)/\1~\2~\3/" | tr "~" "\12"

It gives this output:
  A123456789
  17/02/00
  Mr Smith
  B987654321
  28/02/01
  Mrs Smith

Is that what you want?

Note that this temporarily inserts "~"s in places you will want the line-feeds to be, and then tr converts them to line-feeds.  There might be a way to do this in one step, but I don't know it.
So, the above will not work properly if "~" ever appears in the infile.  In this case, try to change the command to use a character (in place of "~") which WILL never appear in infile.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

752 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