How to read one row at time using shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read one row at time using shell script?
# 1  
Old 08-18-2016
Blade How to read one row at time using shell script?

Hello Team,

I need information on how to read one row at time using shell script. For example i have below data.

Code:
service-description                   servername    warning  critical  mountpoint
disk-usage-tmp  generic-service  test1               80       90        /tmp
disk-usage-var  generic-service   test1               80       90        /var
disk-usage-data  generic-service  test1               80       90       /data


I want to read one row at time from above and create like below configuration for each service-desription

Code:
define service{
       service_description       disk-usage-tmp
       use                          generic-service
       host_name                 test1
       check_command             check_ssh_disk!80!90!/tmp
      
}

Br,Pradeep

Last edited by vgersh99; 08-18-2016 at 11:31 AM.. Reason: code tags, please!
# 2  
Old 08-18-2016
Code:
exec 9<my.file
while in=`line <&9`
do
  set $in
  in=$n
  command with positional parameter $in
done

is one way. If you have $4 instead of $n you will get the 4th item on the line, space delimited.
# 3  
Old 08-18-2016
something along these lines....
awk -f gh.awk myInputFile where gh.awk is:
Code:
FNR==1 {n=split("service_description use host_name check_command",hA, FS);next }
{
  print "define service{"
  for(i=1;i<=n; i++)
    print "\t" hA[i], (i!=n)?$i:("check_ssh_disk!"$(NF-2)"!"$(NF-1)"!"$NF"\n}")
}


Last edited by vgersh99; 08-18-2016 at 11:54 AM..
# 4  
Old 08-18-2016
Quote:
Originally Posted by ghpradeep
I want to read one row at time from above and create like below configuration for each service-desription
These are two different things, yes?

To read a line into a variable VAR do:

Code:
while read VAR ; do
     echo the read line is: $VAR         # this is an example how to use the variable
done < /path/to/your/input.file

If your line has several words you can use the shells field-splitting abilities to read the line into several variables. The first "field" goes to the first variable, the second to the second variable, etc. If there are less input words than variables the last variables will be empty. If there are more input words the last variable will get all the words which are not assigned up to there.

I suggest you try the following to sunderstand the mechanism: create a file with the following content:

Code:
word1
word1 word2
word1 word2 word3
word1 word2 word3 word4
word1 word2 word3 word4 word5

Now try this code with the saved file:

Code:
while read VAR1 VAR2 VAR3 ; do
     echo VAR1=\"$VAR1\" VAR2=\"$VAR2\" VAR3=\"$VAR3\"
done < /path/to/input.file

As a final addition you can use/modify the shell-internal IFS-variable to influence the way the shell defines what a "word" is. Per default the IFS value is a space, which is why in the above example "word1" was going into one variable and "word2" into the next. Suppose you have this file:

Code:
first value here=second value here=third value here

you could read that in, split not at spaces but at equal signs like this:

Code:
while IFS="=" read VAR1 VAR2 VAR3 ; do
     echo VAR1=\"$VAR1\" VAR2=\"$VAR2\" VAR3=\"$VAR3\"
done < /path/to/input.file

I hope this helps.

bakunin
# 5  
Old 08-18-2016
How about this:

Code:
while read desc use server warn crit mount
do
   if [ "$desc" != "service-description" ]
   then
       printf "define service{\n"
       printf "\t%s\t%s\n" "service_description" "$desc"
       printf "\t%s\t%s\n" "use" "$use"
       printf "\t%s\t%s\n" "host_name" "$server"
       printf "\t%s\t%s\n" "check_command" "check_ssh_disk!$warn!$crit!$mount"
       printf "\n}\n"
   fi
done < infile

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 08-19-2016
Hello, Thank you all for reply. using Chubler_XL script, I am getting my output.
# 7  
Old 08-19-2016
Try also

Code:
{ read X; while read SD US HN WM CM CC X
 do cat <<EOF
define service{
        service_description        $SD
        use        $US
        host name  $HN
        check_command      check_ssh_disk!${WM}!${CM}!$CC
}
EOF
        done; } < file
define service{
	 service_description	disk-usage-tmp
	 use	generic-service
	 host name	test1
	 check_command	check_ssh_disk!80!90!/tmp
}
define service{
	 service_description	disk-usage-var
	 use	generic-service
	 host name	test1
	 check_command	check_ssh_disk!80!90!/var
}
define service{
	 service_description	disk-usage-data
	 use	generic-service
	 host name	test1
	 check_command	check_ssh_disk!80!90!/data
}

@wbport: On my linux system, line closes stdin after reading one single line of text, so your proposal in post#2 will enter an infinite loop...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to remove the last characters from each row of csv using shell script

Hi, I've a csv file seperated by '|' from which I'm trying to remove the excess '|' characters more than the existing fields. My CSV looks like as below. HRLOAD|Service|AddChange|EN PERSONID|STATUS|LASTNAME|FIRSTNAME|ITDCLIENTUSERID|ADDRESSLINE1 10000001|ACTIVE|Testazar1|Testore1|20041|||... (24 Replies)
Discussion started by: rajak.net
24 Replies

2. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

3. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

4. Shell Programming and Scripting

Spliting a row to multiple rows using shell script

Please advice script for changing from A to B I'd like to Split a row to multiple rows with 4 columns using shell script. The data of the file is in one row as below that is 28Mbyte size. A> cto10001 0000000 201010 10:52:13 cto10001 0000000 201011 10:52:13 cto10001 0000000 201011 10:52:13... (2 Replies)
Discussion started by: ianpapa
2 Replies

5. Shell Programming and Scripting

Script shell on line and row

Hello, I want to make a script to do this : the file is with line : TOTO TEST1 TOTO TEST2 TITI TEST1 TITI TEST2 TITI TEST3 i want he become : TOTO TEST1 TEST2 TITI TEST1 TEST2 TEST3 (3 Replies)
Discussion started by: safsound
3 Replies

6. Shell Programming and Scripting

How to do row comparison in shell script

Hi, I need help on doing the below thing in shell script. I have a file with millions of rows called "abc.txt". i have another file with millions of rows called "xyz.txt". I would like to do the below operation. Open the abc.txt, read the first line, do some operations on the column... (2 Replies)
Discussion started by: informsrini
2 Replies

7. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies

8. Shell Programming and Scripting

Perl or Shell script to read a transaction log in real time

Hello, I have a Apache webserver running on RedHat. Its primary function is a proxy server for users accessing the internet. I have a transaction log that logs every transactions of every users. For users trying to access certain sites/content the transactions goes into a 302 redirect loop and... (2 Replies)
Discussion started by: bruno406
2 Replies

9. Shell Programming and Scripting

shell script add each row value

Hi, I wrote below small script which will add up each row value from a file and print the total, but it is not.plz let me know what is wrong.script hangs and not displaying anything. ----------------- while read line ; do sum=0; sum=$sum+$line; done echo $sum; exit 0; ------------------... (8 Replies)
Discussion started by: pradeep_script
8 Replies

10. Shell Programming and Scripting

why shell scripting takes more time to read a file

i have done a coding in shell scripting which reads a file line by line and does something....it takes more than 30 seconds to execute for a single file. when i do the same with perl scripting it takes less than a second. is that shell scripting is not efficient while working with large number of... (1 Reply)
Discussion started by: brkavi_in
1 Replies
Login or Register to Ask a Question