Cut two individual position and summ

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Cut two individual position and summ
# 1  
Old 03-03-2017
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

Quote:
1. Should cut 52,5 column from the file
2. Should cut 59,4 column values from the file
3. Step 1 - Step 2 / 100
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

Code:
cut -c 52-56  myfile | awk '{total = total + $1}END{print total}'
cut -c 59-62  myfile | awk '{total = total + $1}END{print total}'


Thanks,
Arun
# 2  
Old 03-03-2017
A sample (~6 lines) input would be helpful...
# 3  
Old 03-03-2017
You want row-by-row sums of the last two rows?

What does your data look like, anyway? You could bundle this up all in one awk if I knew.

Code:
cut -c 52-56  myfile | awk '{ if(L) print L+$1 ; L=$1 }'

# 4  
Old 03-03-2017
On top of what vgersh99 said, why do you deploy two cut and two awk commands, when all can be done in one single awk only?
# 5  
Old 03-03-2017
Yes . Basically I want to

Code:
(52-56  -  59-62)  * 100

on all individual row in the file

Sample Data
Code:
043166251511150314847023310369200085508025354741201002440000000006247447OZ000000001200000100000060CS08014N0000 
079417671505150127847024800058510000008025354921201003850000000006262303OZ000000001200000100000050OO08002N0000
083771661512150315847000000058510000008025354921201003000000000006262303OZ000000001200000100000050OO19032N0000
085905871508150218847000000085510000008025354841301003290000000006288735OZ000000001200000100000050OO20018N0000
304442121513150326847000000058550000008025354921201003450000000006262303OZ000000001200000100000050OO19035M0000

Thanks
Arun




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

Last edited by RudiC; 03-03-2017 at 02:05 PM.. Reason: Changed QUOE to CODE tags.
# 6  
Old 03-03-2017
Code:
awk '{print (substr($0,52,5) - substr($0,59,4)) / 100}' <yourfile>


Last edited by port43; 03-03-2017 at 02:18 PM..
This User Gave Thanks to port43 For This Post:
# 7  
Old 03-03-2017
Yes, good - but shouldn't either substr's length be increased by one?
This User Gave Thanks to RudiC For This Post:
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. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

3. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 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

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

6. UNIX for Dummies Questions & Answers

Parse or cut concat variables to individual values

Hello I need to pass some environment parameters to a datastage job and am getting an error when trying to send the complete concatinated variable. I have decided to parse out just the values and send as parameters but am struggling to find the best way to do this (actually I am not very... (3 Replies)
Discussion started by: LynnC
3 Replies

7. 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

8. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 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

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... (13 Replies)
Discussion started by: HealthyGuy
13 Replies
Login or Register to Ask a Question