Cut output to same byte position | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Cut output to same byte position

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-21-2006
HealthyGuy HealthyGuy is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 4 December 2006, 7:42 AM EST
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Cut output to same byte position

Hi folks

I have a file with thousands of lines with fixed length fields:

sample (assume x is a blank space)

111333xx444TTTLKOPxxxxxxxxx

I need to make a copy of this file but with only some of the field positions, for example I'd like to copy the sample to the follwing: so I'd like to print bytes 4-5 and 15-16 and they be in the same character positions in the new file.

xxx33xxxxxxxxTLxxxxxxxxxxxxxxx

I started looking at cut -b4-5,15-16 but my output is in position 1-4 instead of the same 4-5 and 15-16 with blank spaces everywhere there was one in the original.

Any help would be appreciated.
Sponsored Links
    #2  
Old 11-21-2006
Manish Jha Manish Jha is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 16 June 2009, 1:18 PM EDT
Location: Boston, USA
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
try with awk. Use the substr($0,Starting_Position,Length) function to cut specific byte from the file.

e.g.
111333xx444TTTLKOPxxxxxxxxx


awk '
{
filler1=substr($0,0,3)
fild1=substr($0,4,2)
filler2=substr($0,6,8)
fild2=substr($0,15,2)
filler3=substr($0,17,11)
printf("%s%s%s%s%s",filler1,fild1,filler2,fild2,filler3)
}' file1 > out_file

Note: Adjuct the filed position as per your correct file layout.


--Manish Jha
Sponsored Links
    #3  
Old 11-22-2006
HealthyGuy HealthyGuy is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 4 December 2006, 7:42 AM EST
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Hi Manish

This does print the correct positions but does not fill in the spaces between with the same number of bytes turned to blanks that were in the original. For example, if we take the following 2 lines.

ABC123DEF
GEH456JKL
Say I want to print position 1-2 and 6-8 and want everything in between turned to blank spaces so the byte positions from my input are in the same positions as my output.

Say x is a blank space the output should look like:
ABxxx3DEx
GExxx6JKx

The suggested awk with substr gives me the right substrings but in the wrong position in the new file:
AB3DE
GE6JK

Thanks for any suggestions
    #4  
Old 11-22-2006
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
echo "ABC123DEF" | sed "s/\(.\{2\}\)\(.\{3\}\)\(.\{3\}\).*/\1   \3 /"

sed "s/\(.\{2\}\)\(.\{3\}\)\(.\{3\}\).*/\1 \3 /"

Match the first two char

sed "s/\(.\{2\}\)\(.\{3\}\)\(.\{3\}\).*/\1 \3 /"

Match next three char

sed "s/\(.\{2\}\)\(.\{3\}\)\(.\{3\}\).*/\1 \3 /"

Match next three char followed by the above three char

.* match till end of the line

I added three blanks between \1 and \3 and one blank after \3 to replace the respective char in input with blanks
Sponsored Links
    #5  
Old 11-22-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Is this ok ?


Code:
awk ' { printf "%sxxx%sx\n", substr($0, 1, 2),  substr($0, 6, 3) }'  filename


ABxxx3DEx
GExxx6JKx
Sponsored Links
    #6  
Old 11-22-2006
HealthyGuy HealthyGuy is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 4 December 2006, 7:42 AM EST
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Thanks everyone.

my samples are simplified so what I actually have are lines 300 bytes long.

I need to print bytes 1-3, 203-240, and 260-289, with blank spaces between so the output remains in the same position. This presents a problem with sed because putting 200 spaces in the replace segment doesn't make sense. The suggestions above are fine for my samples which are 10-12 bytes long but not for 300 byte long lines where are only need to print a few bytes across the line.

Interesting issue I have here I never thought would be so tricky to figure out when I started

Thanks for all suggestions
Sponsored Links
    #7  
Old 11-22-2006
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
If you have Python, an alternative
Sample input:
-------------------
111333 444TTTLKOP
122333 444DDDLKOP
422333 4445DDLTlR



Code:
#!/usr/bin/python
start1,end1 = 3,6 #position 4-6
start2,end2 = 14,17 #position 15-17
for lines in open("test.txt"):
        lines = list(lines.strip())
        lines[start1:end1] = " " * (end1 - start1) #sub space at positoin 4-5
        lines[start2:end2] = " " * (end2 - start2)
        print ''.join(lines)

output:

Code:
111     444TTT   P
122     444DDD   P
422     4445DD   R

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
loop with OK or NOK output at the same position slashdotweenie Shell Programming and Scripting 1 12-13-2010 12:18 PM
Perl- Output file is always 0 byte amit1_x Shell Programming and Scripting 5 06-30-2010 08:28 AM
Remove a byte(Last byte from the last line) vinayrao Shell Programming and Scripting 1 02-04-2009 04:18 PM
Check if 2 files are identical byte-to-byte? krishmaths Shell Programming and Scripting 4 09-04-2008 01:37 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



All times are GMT -4. The time now is 11:45 PM.