substract variable from each line in a file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substract variable from each line in a file
# 1  
Old 06-17-2011
substract variable from each line in a file

Hi everyone,

I have a file containing one column and I want to subtract the first value (value of first line) from each line's value.


I tried working on an awk solution with first defining the variable for the value of the first line
awk D='NR==1' pulse_time.txt

and then subtracting this from the first column of the file
awk '{print $1 -D}' pulse_time.txt

But unfortunately it fully ignores the D in that case.

I also tried to find a shell solution by defining the variables and then subtracting them. But it doesn't do what it should (subtraction incorrect).
S=$(head -1 pulse_time.txt)
OP=$(cat pulse_time.txt)

Any help would be highly appreciated!
many thanks in advance!
# 2  
Old 06-17-2011
awk 'NR==1{x=$0;next}{$0=$1 - x}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Assign line from file to variable

Hi, I am new to shell scripting. Need help with the below requirement. I need help to read a log file and line containing word ORA needs to be captured into a variable and the values of the variable need to be inserted into a table. For E.g. file test.sql has below error: ORA-01017:... (3 Replies)
Discussion started by: ricsharm
3 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

4. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

5. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

6. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

7. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

8. Shell Programming and Scripting

[ask]same but not same (variable and line of file)

hi all. I'm new to bash programming. I have a problem with compare bash variable and string from text. for example. # I wanted to get maximum page (in this case 16) cat "page.html" | while read line do # I want to read line with pattern something like this <page>Page 1 of 16</page>... (1 Reply)
Discussion started by: 14th
1 Replies

9. Shell Programming and Scripting

reading the whole line from a file into a variable

Hi, I am doing : while read line do printf "%s\n" ${line} done <datafile.txt but I am not getting each single line from the data file assigned to the variable line (but only tokens/fields at a time). I also tried while IFS= read -r lineI want the whole line assigned or read into the... (2 Replies)
Discussion started by: shri_nath
2 Replies

10. Shell Programming and Scripting

insert line into file using variable

Hi all, I am trying to insert couple of lines before first occurance of line3 which occuring after line 5. So I identified the line 5 line number in the file. Also redirected the all line3 line number to out.txt. Now I have problem in inserting the line using the variable $rep. Please help me... (2 Replies)
Discussion started by: aimmanuel
2 Replies
Login or Register to Ask a Question