Copying characters on each line in a file

Hello,

I would like to copy the first and third char on each line of a file and place them in the 14h and 17th char positions. The file name is listed first and is 6 char's and the dir name is second and also same char size on each line.

The file has thousands of lines.

Initial file contents:

 012345 /dmm/0/0/stat-file
 6789AB /dmm/0/0/stat-file
 CDEF01 /dmm/0/0/stat-file
  

Final contents needed:

 012345 /dmm/00/02/stat-file
 6789AB /dmm/06/08/stat-file
 CDEF01 /dmm/0C/0E/stat-file

Please explain the practical purpose for this request. It seems like something that would be homework.

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.

The purpose of the post is to copy the initial files into categorized directories that are already in place. The file is placed in the dir according to the name.

There are thousands of files names that will be copied.

thanks

Welcome dmm,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these. I know you've been asked before, but you didn't really answer.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • Is speed important, or is this a one-off? Shell commands might be simpler to knock together, but slower for long-term use.

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

Hello,

This is not homework or anything like that. I am trying to copy the files in the first portion of the line to a dir in the second part of the line with the dir locations named according to the file name. There are thousands of files and I already know how to add the copy statements per line and end of line details.

I've tried assigning variables to the first and third char and I can apply them to the rest of the line but I can't seem to get it to work past the first line.

The file names in the first part of the file are always 6 char, and the dir's are all the same except for the char values I'm trying to change.

As you can imagine there are many more things I'm doing with the file but I have them all worked out already and just need to figure out this last step.

thanks

something along these lines:

awk -F'/' '{$3=$3 substr($0,1,1); $4=$4 substr($0,3,1)}1' OFS='/' myFile

thanks VGERSH99,

That worked perfectly !!

Now I can add the remaining line items and proceed with my massive file copy.

-dmm