LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File content by field into variable (https://www.linuxquestions.org/questions/linux-newbie-8/file-content-by-field-into-variable-734996/)

vijayrc 06-23-2009 10:05 AM

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

David the H. 06-23-2009 10:16 AM

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%:}

vijayrc 06-23-2009 01:12 PM

Quote:

Originally Posted by David the H. (Post 3583457)
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

David the H. 06-23-2009 02:10 PM

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.


All times are GMT -5. The time now is 08:46 AM.