reading line by line a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading line by line a variable
# 1  
Old 07-02-2008
Bug reading line by line a variable

Hello,

I have a variable Diskfree and I would like to read it line by line to extract information, I don't know how to get the line

Code:
Diskfree=`df -h | grep dev`

I want to make a loop to get each value of this line, but I don't know how to get the line. I've seen sample with standard input but not a variable

Code:
while "something read line of Diskfree?"
do
dev=`echo $line | awk '{print $1}'`
size=`echo $line | awk '{print $2}'`
...
done

Smilie
# 2  
Old 07-02-2008
Code:
Diskfree=`df -h | grep dev`
for line in $Diskfree
do
  dev=`echo $line | awk '{print $1}'`
  size=`echo $line | awk '{print $2}'`
  ...
done

# 3  
Old 07-02-2008
Code:
 df -h | awk '/dev/ { printf "%s %s\n", $1,$2}'

# 4  
Old 07-02-2008
It doesn't seem to work.

I'd like that $line contain really the line and not one part off the line : one word.
I have several HD to check.
# 5  
Old 07-02-2008
Without variable:

Code:
c=0; df -tfuseblk|while IFS= read -r;do 
  printf "line %d is -->%s<--\n" $((++c)) "$REPLY" 
done

output:

Code:
% c=0; df -tfuseblk|while IFS= read -r;do
  printf "line %d is -->%s<--\n" $((++c)) "$REPLY"
done
line 1 is -->Filesystem           1K-blocks      Used Available Use% Mounted on<--
line 2 is -->/dev/sdb2             60926512  52040560   8885952  86% /media/USB_002<--
line 3 is -->/dev/sdb3             64484908  22401208  42083700  35% /media/USB_003<--
line 4 is -->/dev/sdb1             30876896   6648776  24228120  22% /media/USB_001<-

With variable and here string (ksh93, bash, zsh):

Code:
d=$(df -tfuseblk) c=0
while IFS= read -r; do
  printf "line %d is -->%s<--\n" $((++c)) "$REPLY"
done<<<"$d"

output:

Code:
% d=$(df -tfuseblk) c=0
% while IFS= read -r; do
while>   printf "line %d is -->%s<--\n" $((++c)) "$REPLY"
while> done<<<"$d"
line 1 is -->Filesystem           1K-blocks      Used Available Use% Mounted on<--
line 2 is -->/dev/sdb2             60926512  52040560   8885952  86% /media/USB_002<--
line 3 is -->/dev/sdb3             64484908  22401208  42083700  35% /media/USB_003<--
line 4 is -->/dev/sdb1             30876896   6648776  24228120  22% /media/USB_001<--

With process substitution (ksh93, bash, zsh):

Code:
c=0; while IFS= read -r; do
  printf "line %d is -->%s<--\n" $((++c)) "$REPLY"
done< <(df -tfuseblk)

output:

Code:
% c=0; while IFS= read -r; do
while>   printf "line %d is -->%s<--\n" $((++c)) "$REPLY"
while> done< <(df -tfuseblk)
line 1 is -->Filesystem           1K-blocks      Used Available Use% Mounted on<--
line 2 is -->/dev/sdb2             60926512  52040560   8885952  86% /media/USB_002<--
line 3 is -->/dev/sdb3             64484908  22401208  42083700  35% /media/USB_003<--
line 4 is -->/dev/sdb1             30876896   6648776  24228120  22% /media/USB_001<--

And given your original post, it seams you need something like this:

Code:
{ read 
  while read fs blks used free usepct mnt; do
    printf "dev: %s, size: %d\n" "$fs" "$blks"
  done 
} < <(df -tfuseblk)

output:

Code:
% { read; while read fs blks used free usepct mnt; do
  printf "dev: %s, size: %d\n" "$fs" "$blks"
done;}< <(df -tfuseblk)
dev: /dev/sdb2, size: 60926512
dev: /dev/sdb3, size: 64484908
dev: /dev/sdb1, size: 30876896


Last edited by radoulov; 07-02-2008 at 12:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Reading of variable in a single line command

Hi All, Below is a sample command that I can run without any problem in the command line. Command Line dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt -rw-r--r-- 1 monuser oinstall 0 Jan 18 11:02 ./filename_20130118.txt But once I put that command line in file (list.txt) and... (3 Replies)
Discussion started by: padi
3 Replies

3. Shell Programming and Scripting

EXPECT: Assign variable by reading a line of text from a file

Hi All, I have been using a program on windows called AutoKey. My environment at work is Linux and I have been experimenting with expect. Very powerful. I can move my AutoKey scripts to Linux using Expect once I am educated on how to read from a file using Expect. My application would be... (1 Reply)
Discussion started by: quemalr
1 Replies

4. Shell Programming and Scripting

Help needed in reading line value into variable

I have a file which has data in 8 lines: 10 34 6 1 4 46 67 31 I am trying to read each value into each different variable so that I use these variables in preparing my report. Another option is to dynamically print each line values in the report like below: users with privA:... (2 Replies)
Discussion started by: sarat949
2 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

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

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

8. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

9. UNIX for Dummies Questions & Answers

reading a line into a variable in unix

hi... i need to open a file and read it line by line, and capture that it in to some variable and manipulate... i need to get a line in to a variable please help :confused: (1 Reply)
Discussion started by: lmadhuri
1 Replies
Login or Register to Ask a Question