|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Is this ok ? Code:
awk ' { printf "%sxxx%sx\n", substr($0, 1, 2), substr($0, 6, 3) }' filenameABxxx3DEx GExxx6JKx |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|