Subtract 100 from first field in long list? Simple manipulation?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtract 100 from first field in long list? Simple manipulation?
# 1  
Old 09-14-2005
Subtract 100 from first field in long list? Simple manipulation?

It sounds so easy to do.

I have a file thats laid out like this..


number text text text text (etc about 15 times with various text fields)

I want to take the first field, "number", subtract 100 from it, and then put it back in the file. a simple little manipulation of the first field in the file.

is there an easy way to do this?

So if it looks like


14523 a b c d e f
24245 g h i j k l

it would end up being

14423 a b c d e f
24145 g h i j k
LordJezo
# 2  
Old 09-14-2005
Code:
 awk '{ $1=$1-100; print $0} ' filename

# 3  
Old 09-14-2005
Well that pretty much worked..

One thing though, why does it convert everything to scientific form?

When I do a manipulation on large numbers the results look something like this:

1.12649e+09

Ideas?
LordJezo
# 4  
Old 09-14-2005
Yes. $1 was a string on input - it got run thru a math operation so now it's a number.
Let's try to force it back to a string.
Code:
awk '{ $1=$1-100; $1=sprintf("%s", $1); print $0} ' filename

# 5  
Old 09-14-2005
Quote:
Originally Posted by jim mcnamara
Yes. $1 was a string on input - it got run thru a math operation so now it's a number.
Let's try to force it back to a string.
Code:
awk '{ $1=$1-100; $1=sprintf("%s", $1); print $0} ' filename

Revised:
Code:
awk '{ $1=sprintf("%s", $1-100); print}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

SQL issues comparing Long field to sysdate

I am having hard time with this sql: select partition_name, high_value FROM user_tab_partitions WHERE table_name = 'WNP_TPRESPONSE_INSTANCE' and to_char(high_value) <= to_char(sysdate - 15, 'yyyymm') ; I get an error: ORA-00932: inconsistent datatypes: expected CHAR got LONG... (1 Reply)
Discussion started by: mrn6430
1 Replies

2. Shell Programming and Scripting

Multiple long field separators

How do I use multiple field separators in awk? I know that if I use awk -F"", both a and b will be field separators. But what if I need two field separators that both are longer than one letter? If I want the field separators to be "ab" and "cd", I will not be able to use awk -F"". The ... (2 Replies)
Discussion started by: locoroco
2 Replies

3. Shell Programming and Scripting

Help with number field manipulation

I have a comma separated file containing numbers, I would like to read the file and divide each number by 1024 and create an output file. Input file : 50312.00,3434.05, ,3433.34,124344.00,434343.00, , , Output file: 49.13,3.35,3.35,0,12.05,424.16,0,0 Please click this link: How to... (2 Replies)
Discussion started by: inditopgun
2 Replies

4. UNIX for Dummies Questions & Answers

Manipulation of a list

I have a list of file names, such as below n02-z30-dsr65-terr0.50-dc0.002-8x6drw-csq.msf n02-z30-dsr65-terr0.50-dc0.006-8x6drw-csq.msf n02-z30-dsr65-terr0.50-dc0.010-8x6drw-csq.msf n02-z30-dsr65-terr0.50-dc0.004-8x6drw-csq.msf n02-z30-dsr65-terr0.50-dc0.008-8x6drw-csq.msf I want to... (11 Replies)
Discussion started by: kristinu
11 Replies

5. Shell Programming and Scripting

Simple string manipulation

Hello, I would like to make simple string manipulation but since i am new in shell scripting some strange stuff are uncomprehensible to me: I would like to pick up the name of a file and put it in a variable: shadok@computer:~$a= folder/fil*.dat shadok@computer:~$echo $a... (3 Replies)
Discussion started by: shadok
3 Replies

6. Shell Programming and Scripting

extract a field from a long sentence!

Hi, I want to extract the value of LENGTH column (high-lighted in red) from a file which will have several lines as shown below: <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE... (4 Replies)
Discussion started by: dips_ag
4 Replies

7. UNIX for Dummies Questions & Answers

Simple String Manipulation

Hi, In a BASH script I have a string variable that will be of the form: DOR DOR Die either a 1, 2, or 3 digit number followed by the letter D. I would like to simply remove the letter D (or alternatively trim the variable up to the D) so that all that remains is a number. How can I do... (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

date field manipulation

I have a data file. Seperated by "|". The 19 th filed is a date field that occurs like this 11/02/2001 i need to convert into the below format 2001-11-02 for e.g.. i/p o/p should be can somebody throw some light (5 Replies)
Discussion started by: dsravan
5 Replies

9. Shell Programming and Scripting

Subtract field values

I've got a long logfile of the form network1:123:45:6789:01:234:56 network2:12:34:556:778:900:12 network3:... I've got a similar logfile from a week later with different values for each of the fields eg network1:130:50:6800:10:334:66 network2:18:40:600:800:999:20 network3:... ... (5 Replies)
Discussion started by: Yorkie99
5 Replies

10. Shell Programming and Scripting

Trim leading zeros to make field 6 characters long

Hi all- I've got a file that will have multiple columns. In one column there will be a string that is 10 digits in length, but I need to trim the first four zeros to make it 6 characters? example: 0000001234 0000123456 0000234566 0000000321 output: 001234 123456 234566 000321 (5 Replies)
Discussion started by: Cailet
5 Replies
Login or Register to Ask a Question