LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-23-2009, 11:05 AM   #1
vijayrc
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Rep: Reputation: 0
File content by field into variable


Hi,
A newbie in scripting.
I have a file that has only one record with 3 columns
TOTALS;11111;12345.67:

Just need help in getting field 2 into a variable and field 3 into another variable.

Thx
 
Old 06-23-2009, 11:16 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Field2=$(cut -d \; -f 2 filename)
Field3=$(cut -d \; -f 3 filename)

Assuming the file really only contains the one line.

If you need to strip the trailing colon off of field 3, then you can do this afterwards:

Field3=${Field3%:}

Last edited by David the H.; 06-23-2009 at 03:09 PM. Reason: Fixed typing mistake
 
Old 06-23-2009, 02:12 PM   #3
vijayrc
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
Field2=$(cut -d /; -f 2 filename)
Field3=$(cut -d /; -f 3 filename)

Assuming the file really only contains the one line.

If you need to strip the trailing colon off of field 3, then you can do this afterwards:

Field3=${Field3%:}
still having issues getting values assigned to a variable
 
Old 06-23-2009, 03:10 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Oops. Looks like I made a mistake in my post when typing in the command. The semicolon needs to be escaped with a backslash, not a forward slash. Use "cut -d \; -f 2 filename" for the command.

I've edited my previous post to correct the error.
 
  


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
Default value in a php variable if a form-field is empty? MheAd Programming 2 12-19-2008 07:21 AM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 04:51 PM
replaced variable field within shell script using sed hamimi Linux - Newbie 2 05-28-2008 09:01 AM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 02:41 PM
Can we specify variable field width in a scanf() format string? skie_knite007 Programming 3 05-13-2005 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:48 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