Truncating a number using sed/awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Truncating a number using sed/awk
# 1  
Old 04-10-2007
Truncating a number using sed/awk

Hi all,

I'm trying to truncate a number like the following:

0001060407013900501048239559900600504083525826350002050354795057

I would like to create an output which puts carriage returns every so many characters, giving an output such as:

0001
060407
0139
0
05
010482395599
0060
050408
35258
26
350002050354795057

Is this possible using sed or awk or something?

Many thanks
# 2  
Old 04-10-2007
You have not specified on what basis, the numbers to be split apart,
if the split is in a random fashion, you could try something like this just expand your selection including the substr

Code:
echo "0001060407013900501048239559900600504083525826350002050354795057" | awk 'BEGIN{OFS="\n"} { print substr($0, 0, 2), substr($0, 2, 4) }'

# 3  
Old 04-10-2007
The number is to be truncated according to the character number (.eg. put carriage return after character 4,7,15 etc). Each number I'd like to truncate is delimited in exactly the same way.
# 4  
Old 04-10-2007
Code:
$ echo 0001060407013900501048239559900600504083525826350002050354795057 | awk -v var="4 7 15" ' { 
n=split(var,arr," "); 
for( i = 1; i<= n ; ++i ) { gsub(arr[i],arr[i]"\n"); }  
print } '
00010604
07
01390050104
8239559900600504
083525826350002050354
7
95057

# 5  
Old 04-10-2007
Quote:
Originally Posted by michaeltravisuk
The number is to be truncated according to the character number (.eg. put carriage return after character 4,7,15 etc). Each number I'd like to truncate is delimited in exactly the same way.
You could use the command provided and modify the args to substr and you would have your desired output Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk truncating first field output?

Hello, I'm writing an Awk script to take a command line argument (student's name) and output their relevant student#, name, and marks. For some reason, awk arbitrarily removes the first digit from the student number and doesn't show me the proper output. Here is my code: #! /usr/bin/awk -f... (6 Replies)
Discussion started by: trashmouth12
6 Replies

2. Shell Programming and Scripting

adding a number with sed or awk.

Hi.. I have this delicate problem..:wall: I have this huge ldif file with entry's like this example below.. And I need to change the following entrys. telephoneNumber: emNotifNumber: billingnumber= BillingNumber: Al these entrys has a number like 012345678 and it needs to add one more... (15 Replies)
Discussion started by: pelama
15 Replies

3. Shell Programming and Scripting

count and number instances of a character in sed or awk

I currently use LaTeX together with a sed script to set cloze test papers for my students. I currently pepend and equals sign to the front of the words I want to leave out in the finished test, =perpendicular, for example. I am able to number the blanks using a variable in LaTeX. I would like to... (8 Replies)
Discussion started by: maouinin
8 Replies

4. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

5. Shell Programming and Scripting

sed truncating last line if EOF is not with newline

sed 's|^xyz.abc *= *.*|xyz.abc=60|' /test/myFile.properties > /test/myFile.properties1 myFile.properties has the last line as xyz.abc=23 does not end with newline. When I run this command the last line is truncated if there is no newline character at EOF. Looking for an alternative here. (1 Reply)
Discussion started by: sunn_hps
1 Replies

6. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies

7. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

8. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies

9. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies

10. Shell Programming and Scripting

Limit of number of lines for awk and sed.

Hi, I'm using awk and sed to extract some data out from a text file. The text file consists of data over a million (prolly millions) of lines. Question: Is there a limit of number of lines for awk and sed? Thanks in advance. (2 Replies)
Discussion started by: 60doses
2 Replies
Login or Register to Ask a Question