Cut output to same byte position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut output to same byte position
# 8  
Old 11-23-2006
try this,

though not efficient, some pointers to proceed with Smilie

Code:
#! /bin/zsh

gen()
{
 appVar="x"
 cnt1=$1
 cnt2=$2
 var1=""
 var2=""

 while [ $cnt1 -gt 0 ]
 do
   cnt1=$(($cnt1 - 1))
   var1=$var1$appVar
 done

 while [ $cnt2 -gt 0 ]
 do
gen 3 1

awk '{ printf "%s %s\n", substr($0, 1, 2), substr($0, 6, 3) }' filename | while read line1 line2
do
echo $line1$var1$line2$var2
done

exit 0

   cnt2=$(($cnt2 - 1))
   var2=$var2$appVar
 done
}

# 9  
Old 11-23-2006
Try...
Code:
awk '{for(i=1;i<=length;i++)
        printf ((i>=1&&i<=3||i>=203&&i<=240||i>=260&&i<=289)?substr($0,i,1):"x")
      printf ORS}' file1 > file2

# 10  
Old 11-23-2006
Thanks everyone, this last post from Ygor looks like the easiest option. I've been playing with it this morning but getting a syntax error I can't get past.

awk: syntax error near line 2
awk: illegal statement near line 2


Any ideas?

Thanks all! Smilie
# 11  
Old 11-23-2006
On Solaris, use nawk.
# 12  
Old 11-24-2006
Nawk works better but still not 100%, the code is now:

nawk '{for(i=1;i<=length;i++)
printf((i>=1&&i<=3||i>=203&&i<=240||i>=260&&i<=289), substr($0,i,1))}' file_in > file_out

but my file in looks like
ABC SOLARIS 2200 MAIN STREET

My out file is getting 1's in place of the characters I want to print and 0's in place of the blank spaces I want:
1110000000000011111111100000111111111

I'm sure it's something simple but I'm not seeing it Smilie

Thanks for any input
# 13  
Old 11-26-2006
You have altered the awk script. Why not try the code as posted?
# 14  
Old 11-27-2006
You are correct, thanks Ygor! I was getting a syntax error as awk before so I altered it then changed it to nawk without replacing the changes i had made in awk. It does exactly what I need with a space in place of the "x".

Thanks very much!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut command with dynamic passing of delimiter and position values

Hi All, We have a requirement of picking nth position value by using cut command. value would be delimited by any symbols. We have to pass delimited value and postition to get the value in a string. ex. echo "A,B,C,D,E" |cut -d "," -f3 echo "A|B|C|D|E"|cut -d "|" -f2 Kindly frame the... (5 Replies)
Discussion started by: KK230689
5 Replies

2. UNIX for Beginners Questions & Answers

Print byte position of extended ascii character

Hello, I am on AIX. When I encounter extended ascii characters and special characters on a file I need to print.. Byte position, actual character and line number. Is there a simple command that can give me the above result ? Thanks in advance (38 Replies)
Discussion started by: rosebud123
38 Replies

3. UNIX for Beginners Questions & Answers

Cut two individual position and summ

Hi All, I am having a huge file file. I need to cut the below column and do the below calculation I did the below command and able to do on full databased no on the individual based any help on doing each row by row cut -c 52-56 myfile | awk '{total = total + $1}END{print total}'... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

4. Shell Programming and Scripting

How to cut line from certain position?

Hi Gurus, I want to cut my file from 27th charactor to the end of line. can anybody give some unix command to do this. Thanks in advance (3 Replies)
Discussion started by: ken6503
3 Replies

5. Shell Programming and Scripting

Cut multiple data based on character position

How to extract multiple data based on character position. I need to fetch from 7-9 and 22-26 and there is no delimiter for 22-26 since it is part of the column. The file may have more than 1000 character long.I managed to pull any one but not both for example test data 12345 zxc vbnmlk... (1 Reply)
Discussion started by: zooby
1 Replies

6. Shell Programming and Scripting

Perl- Output file is always 0 byte

Hi all, I am new to perl programming. However i have a script that connects to the database and spools that into an output file. Strange thing is that sometimes this script works and sometimes the ouput spool file is always 0 byte. I have verified the sql query and the query always returns... (5 Replies)
Discussion started by: amit1_x
5 Replies

7. Shell Programming and Scripting

Remove a byte(Last byte from the last line)

Hi All Can anyone please suggest me how to remove the last byte from a falt file .This is from the last line's last BYTE. Please suggest me something. Thank's and regards Vinay (1 Reply)
Discussion started by: vinayrao
1 Replies

8. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server. For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match. Please... (4 Replies)
Discussion started by: krishmaths
4 Replies

9. Shell Programming and Scripting

Cut position as a variable

Hi All, I wish to cut an input by using the below command but i would want to have the cut position as a variable. For eg, the position number is 11 now and i wish that this number is being assigned to a variable before putting into the cut function. Can this be done ? Can any expert help?... (1 Reply)
Discussion started by: Raynon
1 Replies

10. Shell Programming and Scripting

byte count and cut command

1. Is there a way to count the number of bytes of a variable? example: abc@yahoo.com is 13 bytes 2. Cut command only allows one byte for delimiter example: cut -f1 -d'.' delimited by period. Is there a way to have two or more characters in the delimiter field? thanks in adavance. :) (4 Replies)
Discussion started by: hemangjani
4 Replies
Login or Register to Ask a Question