Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Copying characters on each line in a file

Shell Programming and Scripting


Tags
awk, copying char within a file, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-10-2017
dmm dmm is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 4:41 PM EST
Location: Colorado Springs area
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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:


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

Final contents needed:


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


Last edited by joeyg; 01-10-2017 at 09:44 AM.. Reason: Added codetags
Sponsored Links
    #2  
Old Unix and Linux 01-10-2017
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 12 July 2017, 8:05 AM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,444
Thanks: 119
Thanked 190 Times in 171 Posts
What is the purpose of the request?

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.
Sponsored Links
    #3  
Old Unix and Linux 01-10-2017
dmm dmm is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 4:41 PM EST
Location: Colorado Springs area
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
purpose - goal

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
    #4  
Old Unix and Linux 01-10-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 July 2017, 6:43 AM EDT
Location: Lancashire, UK
Posts: 3,167
Thanks: 1,333
Thanked 604 Times in 546 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 01-10-2017
dmm dmm is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 4:41 PM EST
Location: Colorado Springs area
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
!not homework

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
Sponsored Links
    #6  
Old Unix and Linux 01-10-2017
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 20 July 2017, 11:51 PM EDT
Location: Foxborough, MA
Posts: 8,178
Thanks: 220
Thanked 778 Times in 728 Posts
something along these lines:

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

Sponsored Links
    #7  
Old Unix and Linux 01-10-2017
dmm dmm is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 4:41 PM EST
Location: Colorado Springs area
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
worked like a charm

thanks VGERSH99,

That worked perfectly !!

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

-dmm
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
copying a line from a file using sed gotamp Shell Programming and Scripting 4 08-06-2011 07:37 AM
Copying a line from one file to other using vi editor ajincoep Shell Programming and Scripting 2 10-19-2010 09:24 AM
First Three Characters of the first line of a file sandeep_1105 UNIX for Dummies Questions & Answers 8 02-25-2010 06:57 AM
copying from line N1 to line N2 of a file in a series of files paolalup Shell Programming and Scripting 2 06-18-2009 10:47 AM
copying files with dumb characters kaklon Shell Programming and Scripting 2 06-18-2008 07:27 AM



All times are GMT -4. The time now is 12:49 AM.