Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read one line at a time with C shell in unix

Tags:

unix

csh

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]
like image 424
SIMEL Avatar asked Oct 25 '10 09:10

SIMEL


People also ask

How read a specific line in Unix?

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.

How do you read a line one by one in shell script?

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.

How do you read one line at a time in bash?

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

How to read line by line from a file in C?

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.

How to read a file line by line in bash script?

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?

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.

How do you read from the newline?

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


1 Answers

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\$
like image 199
SIMEL Avatar answered Oct 11 '22 20:10

SIMEL