I try to make a small script, using c shell, that will take a file made of several lines, each containing a name and a number and sum all numbers that a have certain name. How can I put into a variable the next line each time?
the summig part I do by: (after I'll be able to get a full line to $line)
set line =($line)
@ sum = $sum + $line[2]
Using the head and tail Commands First, we get line 1 to X using the head command: head -n X input. Then, we pipe the result from the first step to the tail command to get the last line: head -n X input | tail -1.
Syntax: Read file line by line on a Bash Unix & Linux shell file. The -r option passed to read command prevents backslash escapes from being interpreted. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed. while IFS= read -r line; do COMMAND_on $line; done < input.
We can use the read command to read the contents of a file line by line. We use the -r argument to the read command to avoid any backslash-escaped characters. In the following example, we can see that we have an iteration over a file line by line and we store the contents of a single line in the variable “line“.
Use the following program for getting the line by line from a file. If you are coding for a platform that has the GNU C library available, you can use getline (): The fgets function will read a single line from a file or num characters where num is the second parameter passed to fgets.
Create a bash file and add the following script. This script will take the filename from the command line argument. First argument value is read by the variable $1 which will contain the filename for reading. If the file exists in the current location then while loop will read the file line by line like previous example and print the file content.
How to: Read a Text File One Line at a Time (Visual C#) This example reads the contents of a text file, one line at a time, into a string using the ReadLine method of the StreamReader class. Each text line is stored into the string line and displayed on the screen. Example. int counter = 0; string line; // Read the file and display it line by line.
Use fgets to read from the line, and then use getc (...) to chew up the newline or end-of-line to continue reading....here's an example of forever reading a line...
I have managed to solve it using the next piece of code:
foreach line ("`grep $1 bank`")
echo $line
set line_break = ($line)
@ sum = $sum +$line_break[2]
end
echo $1\'s balance id: $sum\$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With