Shift record from one position to another


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 6 Days Ago
Shift record from one position to another

Hi All,

I have a file and it is a fixed length file. I want to move the values from 42,6 ( where 6 is length) to the 36th position

Code:
Original file:
00000100000100000100000100000100001      000870                                                                  
00001000001000001000001000001000001      000879                                                                                  
09000001000001000001000001000001001      001818                                                             
29000020900000100000100000100000001      001816                                           
29000020900000100000100000100000001      001817
Expected : 
00000100000100000100000100000100001000870                                                                  
00001000001000001000001000001000001000879                                                                                  
09000001000001000001000001000001001001818                                                             
29000020900000100000100000100000001001816                                           
29000020900000100000100000100000001001817

Things I have tried.
I am doing a cut on 42,6 and storing it in temp file then doing cut on 1,36 and the merging the file. Not working

Code:
cut -c1-36 file1
cut c46-52  file2

join file1 file2 > file3 -- not giving right result
Paste -s  file1 file2 -- giving me all result in one line formate
cat file1 file2 --->appending the file at end

Thanks in advance
# 2  
Old 6 Days Ago
Try this

Code:
sed 's/      //g' file1 > output.file

--- Post updated at 05:47 AM ---

Try below :-

Code:
sed 's/      //g' file1 > output.file

Code:
tr -d ' ' <file1 > output.file


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 6 Days Ago at 08:38 AM.. Reason: Added CODE tags.
# 3  
Old 6 Days Ago
Thanks for the input . I should have been more clear. This sed will convert all blank space to non blank. There is a change that other position can be blank. I need to move these only based on position
# 4  
Old 6 Days Ago
cut

Code:
cut -c1-35,42- data.txt

sed
Code:
sed -re 's/(.{35}).{6}/\1/' data.txt

Perl

Code:
perl -pe 's/^(.{35}).{6}/$1/' data.txt

GNU(!) awk

Code:
awk 'match($0,/^(.{35}).{6}(.*)/,res){print res[1] res[2]}' data.txt


Last edited by stomp; 6 Days Ago at 08:43 AM..
These 4 Users Gave Thanks to stomp For This Post:
arunkumar_mca (6 Days Ago) Corona688 (5 Days Ago) Don Cragun (4 Days Ago) RudiC (6 Days Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Inserting value at a particular position without changing the position of other characters rahulsk Shell Programming and Scripting 2 03-31-2017 06:27 AM
Need code for updating second record to first record in shell scripting Samah Shell Programming and Scripting 1 06-29-2016 08:22 AM
Search for a string at a particular position and replace with blank based on position Pradhikshan Shell Programming and Scripting 9 09-16-2015 10:17 PM
Extract timestamp from first record in xml file and it checks if not it will replace first record vsraju Shell Programming and Scripting 2 11-06-2014 10:27 PM
Need command or script to print all lines from 2nd position to last but one position hemanthsaikumar Shell Programming and Scripting 2 07-31-2014 05:20 AM
Require original field position after sorting the values in a record Srinivasa Reddy Shell Programming and Scripting 3 10-10-2013 01:13 AM
How to compare current record,with next and previous record in awk without using array? Dona Clara Shell Programming and Scripting 7 04-15-2013 03:56 AM
Search and Replace by record position macastor Shell Programming and Scripting 11 11-02-2011 01:43 PM
Reject the record if the record in the next line does not begin with 2. supchand Shell Programming and Scripting 5 10-16-2011 04:52 PM
Reject the record if the record in the next line does not satisfy the pattern supchand Shell Programming and Scripting 2 10-16-2011 01:15 AM
Remove text from n position to n position sed/awk elamurugu Shell Programming and Scripting 5 12-19-2010 07:06 PM
Moving first position in a file to the last position pparthiv Shell Programming and Scripting 11 06-28-2010 11:13 PM
find if a position is between a given start and end position fadista UNIX for Dummies Questions & Answers 9 12-04-2008 11:17 AM
how to find a position and print some string in the next and same position naveenkcl Shell Programming and Scripting 1 08-21-2008 01:18 PM
Add 'ENDEND' on end of each record at position is 14-20 naveenkcl Shell Programming and Scripting 5 08-21-2008 05:18 AM