LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to parse a line of text (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-parse-a-line-of-text-877520/)

rkinch 04-28-2011 12:46 AM

How to parse a line of text
 
I want to write a bash script to parse a text file with the following lines and set variables for each line so that I can use them in the rest of the script.

Timestamp=123456789
Company=ABC Company
Server=Server
Recipient=Joe Smith
Email=joe@abc.com

Can someone tell me the simplest way to read each line one at a time for everything before the =, set that to a variable name with the value equal to everything after the =

mayursingru 04-28-2011 01:27 AM

Hi rkinch,
Could you please elaborate your point further. I came up with a solution which is given below if it works let me know.

Code:

Input
 Timestamp=123456789
Company=ABC Company
Server=Server
Recipient=Joe Smith
Email=joe@abc.com

Code:

awk 'BEGIN {FS=" "} { print $1 }' file

Code:

output
Timestamp
Company
Server
Recipient
Email


grail 04-28-2011 01:35 AM

Simplest:
Code:

. file

mayursingru 04-28-2011 01:37 AM

Hi rkinch,
Just a little modification in my code.
Code:

  awk 'BEGIN {FS="="} { print $1 }' file

replica9000 04-28-2011 01:41 AM

Code:

cat file | cut -d '=' -f 2

colucix 04-28-2011 04:03 AM

Quote:

Originally Posted by grail (Post 4338842)
Simplest:
Code:

. file

Hi grail! :) Indeed this is the simpliest solution, but it should preceded by a command which adds double quotes around the RHS, due to the presence of spaces. For example (using eval as alternative to the source command):
Code:

$ eval "$(sed -r 's/(=|$)/\1"/g' file)"
$ echo $Timestamp
123456789
$ echo $Company
ABC Company


grail 04-28-2011 04:24 AM

My bad ... just saw LHS=RHS and jumped ... :doh: .. that'll teach me lol

rkinch 04-28-2011 09:28 AM

(Elaborate)

Thanks for your ideas so far. I have to experiment with them. I'm not new to scripting in general, but I am new to Linux scripting, so I don't know which commands to use to best get the job done.

My end game here is to monitor whether a Windows server is up and running by having it send a text file periodically via FTP to my Linux system. The Linux script will read the timestamp from the text file and compare it to it's current timestamp to see how much time has passed. If more than say 15 minutes has passed since the timestamp has been updated by the remote server, then I want to use the variables from the text file and send an email to the recipient letting them know that their server may be down.

So I want to read the text file and make each line into a Linux variable and then compare the current time stamp against the one in the text file variable (I don't know yet if I can directly compare a Windows time stamp with a Linux one) and then send out an email if needed.

grail 04-28-2011 09:41 AM

Well I guess my question back would be ... how is the file being generated?

ie. If you could place single or even double quotes around everything on the RHS then sourcing the file, ie the dot in my simple solution,
will turn all items on the LHS into variables that could be used in a script.

Or you could read the file through a loop maybe using something like awk to maybe split on the equals and provide the 2 halves that could be tested
and then used accordingly.

Ultimately there are heaps of choices :)

rkinch 04-28-2011 09:45 AM

$ eval "$(sed -r 's/(=|$)/\1"/g' server.log)"

I tried out the above command from the command line and the result was that it listed each line of the text file and at the end of the last line it said "command not found". I could not echo the variables out.

I'd like the command to set each line as shell variable so I can use them. Not sure why the command not found. I'm using CentOS 5.5 BTW.

Thanks

rkinch 04-28-2011 09:57 AM

Grail - I'm going to generate the file using a vbscript on the Windows server and then FTP it to the Linux server. I can put anything I need to in that file, so if quotes would help, that shouldn't be a problem.

So then I would want the file to have(?):

Timestamp="1232456"
Company="Company Name"
Server="Server Name"
etc... ???

rkinch 04-28-2011 10:04 AM

I tried the suggestion of putting the quotes around the RHS info and it works beautifully. I can't believe how easy that was. Thank you

Now I just have to figure out how to compare the Windows timestamp against the current linux timestamp. If you have any suggestions there I would appreciate it. Again, I have complete flexibility in how I generate the timestamp variable on the Windows side.

replica9000 04-28-2011 10:12 AM

Code:

stat -c "%x" ./file
will show you the time stamp

grail 04-28-2011 10:25 AM

I am guessing by timestamp on linux you might be referring to the actual current time on the linux machine?

In that case you need to play with the date command and specifically the -d switch which allows you to choose a format.
This way you can then play on both sides until you get a match for format.

If incorrect and you actually need a file timestamp, then go with replica's suggestion :)

speedy64 04-28-2011 11:10 AM

Instead of windows FTPing a file to Linux server then running a compare process. Why not ftp "anything" to Windows server? If FTP session is not responsive send email from Linux server.


All times are GMT -5. The time now is 03:30 AM.