LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2011, 11:46 PM   #1
rkinch
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Rep: Reputation: 0
Question 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 =
 
Old 04-28-2011, 12:27 AM   #2
mayursingru
Member
 
Registered: Nov 2010
Location: Pune
Distribution: CentOS
Posts: 51

Rep: Reputation: 5
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
 
Old 04-28-2011, 12:35 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Simplest:
Code:
. file
 
Old 04-28-2011, 12:37 AM   #4
mayursingru
Member
 
Registered: Nov 2010
Location: Pune
Distribution: CentOS
Posts: 51

Rep: Reputation: 5
Hi rkinch,
Just a little modification in my code.
Code:
  awk 'BEGIN {FS="="} { print $1 }' file
 
Old 04-28-2011, 12:41 AM   #5
replica9000
Senior Member
 
Registered: Jul 2006
Location: USA
Distribution: Debian, FreeBSD, Android
Posts: 1,032
Blog Entries: 2

Rep: Reputation: 215Reputation: 215Reputation: 215
Code:
cat file | cut -d '=' -f 2
 
Old 04-28-2011, 03:03 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by grail View Post
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
 
Old 04-28-2011, 03:24 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
My bad ... just saw LHS=RHS and jumped ... .. that'll teach me lol
 
Old 04-28-2011, 08:28 AM   #8
rkinch
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Original Poster
Rep: Reputation: 0
(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.
 
Old 04-28-2011, 08:41 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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
 
Old 04-28-2011, 08:45 AM   #10
rkinch
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Original Poster
Rep: Reputation: 0
$ 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
 
Old 04-28-2011, 08:57 AM   #11
rkinch
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Original Poster
Rep: Reputation: 0
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... ???
 
Old 04-28-2011, 09:04 AM   #12
rkinch
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 04-28-2011, 09:12 AM   #13
replica9000
Senior Member
 
Registered: Jul 2006
Location: USA
Distribution: Debian, FreeBSD, Android
Posts: 1,032
Blog Entries: 2

Rep: Reputation: 215Reputation: 215Reputation: 215
Code:
stat -c "%x" ./file
will show you the time stamp
 
Old 04-28-2011, 09:25 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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
 
Old 04-28-2011, 10:10 AM   #15
speedy64
LQ Newbie
 
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7

Rep: Reputation: 0
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.

Last edited by speedy64; 04-28-2011 at 10:12 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
parse text between html wakatana Programming 4 10-27-2009 08:12 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
Can you parse text with regex? raypen Slackware 4 03-21-2006 12:26 PM


All times are GMT -5. The time now is 06:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration