Need to copy data from one position to another in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need to copy data from one position to another in file
# 1  
Old 01-11-2012
Need to copy data from one position to another in file

Hi.

I need to write a script that will allow me to copy data from one position in a line to another position while changing the first 2 bytes of the data to a constant. Here is an example of a line of data before and what it needs to look like after. there are about 200 lines in the file.

before
COPY FROM CC.CODES F11PFASD

after
COPY FROM CC.CODES F11PFASD,F12PFASD

I was trying to figure out a way to do using awk or sed but cannot figure it out. Any other ideas are welcome.

Thank you.
# 2  
Old 01-11-2012
Well, if the word you want to duplicate is always the last one, and F11 is to be replaced with F12 (three characters not two as you indicated in your text), then this will do it for you:

Code:
awk '
    {
        n = $NF;
        sub( "^F11", "F12", n );
        printf( "%s,%s\n", $0, n );
    }
' input-file >output-file

This User Gave Thanks to agama For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash copy and paste text in file from one position to another

Hi I have a text file with lines beginning with 71303, 71403, 71602, I need to copy the 10 digit text at position 30 on lines beginning with 71303 (5500011446) to position 99 on every line beginning with 71602 (see example below), There may be many 71303 lines but I need the text copying to... (2 Replies)
Discussion started by: firefox2k2
2 Replies

2. Shell Programming and Scripting

Column to row and position data in a text file

Hi everyone.. I have a list of values in a file... a, b, c, 1, 2, 3, aaaa, bbbbb, I am interested in converting this column to a row.. "text",aaaa, bbbb a,1 (7 Replies)
Discussion started by: manihi
7 Replies

3. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

4. Shell Programming and Scripting

Copy same name data from other file base on column

HI I have input file A.txt X Y Z File B.txt 1 X 10 AAA 11123 2 Y 22 PlD 4563 3 Z 55 PlD 54645 4 Z 66 PlD 15698 5 F 44 PlD 154798 6 C 55 PlD 12554 7 Z 88 PlD 23265 8 C 99 PlD 151654 9 C 11 PlD 21546546 I need New File C.txt (1 Reply)
Discussion started by: pareshkp
1 Replies

5. Shell Programming and Scripting

Add '|' to data file on particular position

I have below scenario where i am getting a flat file but with some bad data incoming data format id|name|ind|crncy 123|xxx|y|usd 234|yy|n| 456|a|y90.5|vvv|gbp ----bad dataneed to cleanse the bad data above by adding a pipe '|' after 3rd column 'ind' if pipe '|' is not already there ... (1 Reply)
Discussion started by: rakesh5300
1 Replies

6. Shell Programming and Scripting

copy data from different file with some same column

I have file1 have below data ABCDE2012 ABCDE2012120 -099.8 -099.4 00.4 ABCDE2012 ABCDE2012124 -104.6 -103.6 01.0 ABCDE2012 ABCDE2012128 -104.4 -103.1 01.4 file2 ABCDE2012 ABCDE2012120 15.4 ABCDE2012 ABCDE2012124 18.5 ABCDE2012 ABCDE2012128 20.1 ABCDE2012 ABCDE2012122 0.5 ... (1 Reply)
Discussion started by: pareshkp
1 Replies

7. Shell Programming and Scripting

copy content of file to another files from certain position

Hello Guys I have a directory of xml files (almost 500 ) Now in each xml file 3 new attributes will be added which are in a different single file Is there any way I can copy the whole content of single file at 6th line of all the files in the directory . Thanks a lot!!! (7 Replies)
Discussion started by: Pratik4891
7 Replies

8. Shell Programming and Scripting

Copy an entire file to specific position to another file

Hi , I need your kind help for my below requirement I need to copy and entire txt file to a certain position to the target file . Source file has 3 lines and it has to be copied to the target file in position from line 10 to 12. Thanks for your support (1 Reply)
Discussion started by: Pratik4891
1 Replies

9. Shell Programming and Scripting

how to copy data to to excel file

Hi, Can any one tell me how to copy data using shell script to a excel file from text file to other columns of excel file,leaving first column unaffected i.e it should not overwrite data in first column. Say my text file data is: 15-dec-2008 15-dec-2009 16-dec-2008 16-dec-2009 ... (7 Replies)
Discussion started by: tucs_123
7 Replies

10. HP-UX

find the position in a file and insert the data there

Hi, I have a EDI data file ARROWTEST of size 18246 characters. And I want to insert some data after 4200 position in the file. How I can find the position 4200 in that file....Please advise. Regards, (5 Replies)
Discussion started by: isingh786
5 Replies
Login or Register to Ask a Question