LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-08-2016, 03:49 PM   #1
eco_bach
Member
 
Registered: Dec 2016
Posts: 242

Rep: Reputation: Disabled
Parse text file to retrieve values from BASH


Lets say I have a file file_list.txt

This file contains the following

Code:
File ID: 81
Filename: WP_20161029_16_26_49_Pro.jpg
File size 936160 (0x00000000000E48E0) bytes
Parent ID: 12
Storage ID: 0x00010001
Filetype: JPEG file

How would I obtain the File ID and the Parent ID ?
 
Old 12-08-2016, 04:30 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
What do you mean "from BASH" ?.
What have you tried - a simple search should provide plenty of candidates. Personally I'd be inclined to awk.
 
Old 12-11-2016, 01:23 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
awk has the advantage of a flexible field separator.
Example
Code:
awk -F ": *" '
($1=="File ID") { print "filed=" $2 }
($1=="Parent ID") { print "parentid=" $2 }
' file
 
Old 12-11-2016, 11:04 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,615

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by eco_bach View Post
Lets say I have a file file_list.txt

This file contains the following

Code:
File ID: 81
Filename: WP_20161029_16_26_49_Pro.jpg
File size 936160 (0x00000000000E48E0) bytes
Parent ID: 12
Storage ID: 0x00010001
Filetype: JPEG file

How would I obtain the File ID and the Parent ID ?
There are, literally, DOZENS of ways that might pertain. BASH alone is not optimal for this, but there are many pattern-matching tools it can call to assist. There are also tricks using recursion and parameter lists, arrays, and cascading conditions that could be used. Might I ask what you have tried, and what you mean by 'obtain' in this context? What is your ultimate goal?

Isolating the values is one thing, how you use them once isolated is another.
 
Old 12-11-2016, 03:15 PM   #5
Fat_Elvis
Member
 
Registered: Oct 2016
Distribution: FreeDOS 1.2
Posts: 309

Rep: Reputation: 92
If the variables are in the same order every time, I'd do:

Code:
FILE="$(cat file_list.txt)"
FILE_ID="${FILE%%$'\n'*}"
FILE_ID="${FILE_ID##*ID: }"
PARN_ID="${FILE##*Parent ID: }"
PARN_ID="${PARN_ID%%$'\n'*}"
I'd also suggest using the above awk solution, which is probably faster than this.

Last edited by Fat_Elvis; 12-11-2016 at 03:16 PM.
 
1 members found this post helpful.
Old 12-11-2016, 04:06 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
If that is all that is in the file, then:

Code:
file_id=$(grep "File ID" file_list.txt|awk '{print $3}')
parent_id=$(grep "Parent ID" file_list.txt|awk '{print $3}')
 
Old 12-12-2016, 09:25 AM   #7
eco_bach
Member
 
Registered: Dec 2016
Posts: 242

Original Poster
Rep: Reputation: Disabled
Thanks! Going with awk
 
Old 12-12-2016, 12:49 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by MadeInGermany View Post
awk has the advantage of a flexible field separator.
Example
Code:
awk -F ": *" '
($1=="File ID") { print "filed=" $2 }
($1=="Parent ID") { print "parentid=" $2 }
' file
The output is shell syntax, so a simple eval assigns to shell variables
Code:
eval $(
awk -F ": *" '
($1=="File ID") { print "file_id=" $2 }
($1=="Parent ID") { print "parent_id=" $2 }
' file
)
echo $file_id
echo $parent_id
 
  


Reply



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
extracting values from web page to simple text file (inside a bash script) ondoho Linux - General 5 06-29-2014 11:51 AM
[SOLVED] Need to parse numeric values and file paths from a security scan output. BobbyinMDC Programming 9 08-27-2012 10:06 AM
[SOLVED] Retrieve numerical values from a text file shik28 Programming 1 11-22-2011 02:02 AM
Parse multiple variable from text file with bash Goni Programming 2 07-13-2010 02:25 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:39 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
Open Source Consulting | Domain Registration